Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6064035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:10+00:00 2026-05-23T09:14:10+00:00

I have a Player class that uses a ForeignKey to a Photo object for

  • 0

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 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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:14:11+00:00Added an answer on May 23, 2026 at 9:14 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a flash player that has a set of songs loaded via an
I have an XML object that I want to send to my player via
I have that template class that uses a policy for it's output and another
I have a class that contains player numbers... public class Game { public int
I have a Player class that derives from Humanoid and Humanoid derives from Sprite
I have a Player class that extends CCNode, and i want to hava a
I have three (C++) classes: Player, Hand, and Card. Player has a member, hand,
We have a custom-built Flash-based video player that I maintain, and it needs to
I have a website that plays mp3s in a flash player. If a user
I have the following 3 classes in my dbml file: public class Player {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.