PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How to save Username instead of Object name(None) automatically while saving the model in django

 October 19, 2022     admin, database, django, django-models, python     No comments   

Issue

check the updated pic

When I save the model, The object name is None, but I need to save the username instead of object name (None) automatically while I saving the Model

here is pic of admin site

Models.py

class solo_21_6_2021(models.Model):
    user = models.OneToOneField(User,null=True,on_delete=models.CASCADE)
    player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
    player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)
 

    def __str__(self):
        return str(self.user)

Views.py

def solo(request):
   form = SoloForm()
   if request.method=="POST":
      form=SoloForm(request.POST)
      if form.is_valid():
          form.save()

   return render(request, 'bgmiapp/solo.html',{'form':form})

Forms.py

    class SoloForm(forms.ModelForm):
         class Meta():
            model = solo_21_6_2021
            fields=['player1_pubg_id','player1_pubg_name'

Admin.py

    class SoloAdmin(admin.ModelAdmin):
        list = ('user','player1_pubg_id','player1_pubg_name')


    admin.site.register(Profile)
    admin.site.register(solo_21_6_2021,SoloAdmin)

Solution

you have set user field to null so the model is created without it, so when you want to show objects by user, it does not have this info so it shows None.

modify models to :

class solo_21_6_2021(models.Model):
  user = models.OneToOneField(User,on_delete=models.CASCADE)
  player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
  player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)

  def __str__(self):
      return str(self.user)

so now it is required and related as it is supposed to be

and inf the forms:

class SoloForm(forms.ModelForm):
    class Meta():
        model = solo_21_6_2021
        fields=['user','player1_pubg_id','player1_pubg_name']

so it display the choice of user in the html form

now when you submit the form, the user name will display in the admin page

*Alternatively you could make your authenticated user the one who is creating the object, so no need to show list of your users to everyone:

def solo(request):
    form = SoloForm()
    if request.method=="POST":
        form=SoloForm(request.POST)
        if form.is_valid():
            f = form.save(commit=False)
            f.user = request.user
            f.save()

return render(request, 'bgmiapp/solo.html',{'form':form})

and get rid of user in forms:

class SoloForm(forms.ModelForm):
    class Meta():
        model = solo_21_6_2021
        fields=['player1_pubg_id','player1_pubg_name']

Ps: since you have a OnetoOne relation, you can create only one object by user, creating another will throw a user_id unique constraint error



Answered By - taha maatof
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing