This is a question about django ForeignKey.

Asked 2 years ago, Updated 2 years ago, 42 views

The first question is what is the difference between the code ForeignKey below? One in the company says, "It's marked, but I don't know what's different. The result is It comes out the same, but I was curious because there were people who used it and people who didn't.

 class register(models.Model): #membership form
        user=models.OneToOneField(User,on_delete=models.CASCADE) #id/PW/Email
        company=models.ForeignKey(Company) #CompanyName

   class Register (models.Model): #MemberRegistrationForm
        user=models.OneToOneField(User,on_delete=models.CASCADE) #id/PW/Email
        company=models.ForeignKey("Company") #CompanyName

The second question is Save ForeignKey. This is from models.py.

class register(models.Model): #membership form
    user=models.OneToOneField(User,on_delete=models.CASCADE) #id/PW/Email
    company=models.ForeignKey(Company) #CompanyName
    office=models.CharField(max_length=30) #Branch and Department

     def __str__(self):
            return self.company+" "+self.office




class Apply(models.Model):
    register=models.ForeignKey(Register,null=True)
    serialnumber=models.CharField(max_length=30) #SerialNumber
    name=models.CharField(max_length=30) #RecipientName

forms.This is what the py says.

class ApplyForm(forms.ModelForm):
class Meta:
    model = Apply
    exclude = ['register']
    fields = ('serialnumber','name')
def save(self, commit=True, id=0):
    apply = super(ApplyForm, self).save(commit=False)
    if commit:
        apply.register=Register.objects.get(pk=id)
        apply.save()
    return apply

This is from views.py.

def apply(request): #application screen
    if request.method == 'POST':
       appfrom = ApplyForm(request.POST)
       if appfrom.is_valid():
         appsave=appfrom.save(commit=False,id=request.user.id)
         appsave.save()
         return HttpResponseRedirect('/lookup/')
    else:
         appfrom = ApplyForm()
    return render(request, 'as/apply.html', {'appfrom': appfrom})

The result image

The goal is to print company and office on that table register.

At the moment, I have to choose between those two, but what I want is a member's decision It was to make the department automatically selected, but isn't it just saved?

django

2022-09-22 11:31

1 Answers

(1) Looking at the models.ForeignKey's init code , you can put the model or model's name. I don't think both of them matter.

(2) Before Apply.save(), Apply.register = Register.objects.get(id)>. If you don't save it, it won't be reflected in the DB.

Additionally.

Apply = super(ApplyForm, self).save(commit=False)

is

apply = super(ApplyForm, self).save(commit=False)

I think it would be better if you changed it to . I'm already using Apply as a model name, so it's different.

Apply.register=Register.objects.get(pk=id)

Try to modify it like this. And id doesn't seem to be defined anywhere When you call save on views.py, please call it with form.save(id=request.user.id) and save should receive the id as follows.

def save(self, commit=True, id=0):


2022-09-22 11:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.