2011-06-23 7 views
0

私は、ムーグショットを添付するためにPhotoオブジェクトにForeignKeyを使用するPlayerクラスを持っています。私の問題は、マグショットを持っているプレイヤーを編集して新しいムービーをアップロードしないと、古いマグショットがPlayerオブジェクトから切り離されてしまうことです。私の論理には何か問題があるはずです。オブジェクト編集時に古いファイルを保存し、Djangoフォームに新しいファイルをアップロードしない

class Player(models.Model): 
    ... 
    mugshot = models.ForeignKey('content.Photo', null=True, blank=True) 
    ... 

マイPlayerFormクラスは次のようになります。私の編集ビューで

class PlayerForm(forms.ModelForm): 
    ... 
    mugshot = forms.FileField(widget=forms.FileInput(), required=False) 
    ... 

    def save(self, commit=True, request=None, user=None): 
     player = super(PlayerForm, self).save(commit=False) 

     if commit: 
      player.save() 

     return player 

、私はプレーヤーを更新し、その後、mugshotを交換するかどうかを確認するには、ファイルのアップロードが存在するかどうかを確認。

def edit_player(request, team_season_id, player_id): 
    team_season = get_object_or_404(TeamSeasonConference, pk=team_season_id) 
    player = get_object_or_404(Player, pk=player_id) 

    if request.method == 'POST' and 'cancel' not in request.POST: 
     player_form = PlayerForm(request.POST, instance=player) 

     if player_form.is_valid(): 
      updated_player = player_form.save(True) 

      if request.FILES: # if sent a file 
       file_data = request.FILES['mugshot'] 
       picture = Photo.objects.create(# create a photo object with that file 
       name = player_form.cleaned_data['first_name'] + ' ' + player_form.cleaned_data['last_name'], # and give it the name of the player you're adding 
       cutline = '', 
       file = file_data, 
       source = '', 
      ) 
     else: 
      picture = player.mugshot; # keep the old one 

     updated_player.mugshot = picture 
     updated_player.save() 

答えて

1

若干の手直しが問題を修正する必要があります。

def edit_player(request, team_season_id, player_id): 
team_season = get_object_or_404(TeamSeasonConference, pk=team_season_id) 
player = get_object_or_404(Player, pk=player_id) 

if request.method == 'POST' and 'cancel' not in request.POST: 
    player_form = PlayerForm(request.POST, instance=player) 

    if player_form.is_valid(): 
     updated_player = player_form.save(True) 

     if request.FILES: # if sent a file 
      file_data = request.FILES['mugshot'] 
      picture = Photo.objects.create(# create a photo object with that file 
       name = player_form.cleaned_data['first_name'] + ' ' + player_form.cleaned_data['last_name'], # and give it the name of the player you're adding 
       cutline = '', 
       file = file_data, 
       source = '', 
      ) 

      # You only need to save again if there is a picture 
      updated_player.mugshot = picture 
      updated_player.save() 

また、フォームは、既存のmugshot除外する必要があります:

class PlayerForm(forms.ModelForm): 

    class Meta: 
     model = Player 
     exclude = ('mugshot',) 

    def save(self, ...): 
     #as it was before 
+0

をそれが安全Playerに以前接続写真を続けるだろうモデル?既存のプレーヤーを編集する際に、ユーザーがファイルをアップロードしない場合、保存したフォームは以前に添付された写真を削除しないでしょうか? – wmfox3

+0

新しいフォームのコードを追加するのを忘れてしまった!フォームから既存のmugshotを除外することにより、既存の外部キーが手動でインスタンスに実行されない限り、変更されないようにします。新しいマグショットがアップロードされたときにのみこれを行います。 –

+0

絶対に!良い解決策。 – wmfox3

関連する問題