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
(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):
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.