开发者

Preserving old file when editing object and not uploading a new one on Django form

I have a Player class that uses a ForeignKey to a Photo object for purposes of attaching a mugshot. My problem is that when I edit a player who has a mugshot and I don't upload a new one, the old mugshot is getting detached from the Player object. There must be something wrong with 开发者_JS百科my logic.

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

My PlayerForm class looks like this:

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

In my edit view, I update the player, then check for the existence of a file upload to see whether or not to replace the 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()


A slight reworking should fix the problem:

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()

Also, your form needs to exclude the existing mugshot:

class PlayerForm( forms.ModelForm ):

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜