我正在尝试制作3个外键的formset。服务员,Sdescrition和雄鹿。出于某种原因,它给了我这个错误
ManagementForm数据丢失或被篡改
我对编码很新,如果有一个比inlineform_factory更好的方法请告诉我并建议。非常感谢你。我几乎试图将三个外键连接在一起,将它们放在网页上的单个表单页面中。再一次,谢谢
Views.py
def home(request):
title = 'Please Enter Company Information'
SdescriptionFormSet = inlineformset_factory( Servicer, Sdescription, form = InputFormSdescription, extra=1)
StagFormSet = inlineformset_factory( Sdescription, Stag, form = InputFormStag, extra=1) #new
Sdescription_formset = InputFormSdescription()
Stag_formset = StagFormSet()
if request.POST:
FormServicer = InputFormServicer(request.POST or None)
FormSdescription = InputFormSdescription(request.POST or None) #new
if FormServicer.is_valid() and FormSdescription.is_valid():
servicer = FormServicer.save(commit=False)
Sdescription_formset=SdescriptionFormSet(request.POST, instance =servicer)
sdescription = FormSdescription.save(commit=False) #new
Stag_formset=StagFormSet(request.POST, instance =sdescription) #new
if Sdescription_formset.is_valid():
servicer.save()
Sdescription_formset.save()
sdescription.save() #n
Stag_formset.save() #new
return HttpResponseRedirect(reverse('serv.views.home'))
else:
FormServicer = InputFormServicer()
Sdescription_formset = SdescriptionFormSet(instance=Servicer())
Stag_formset=StagFormSet(instance=Sdescription())
context = {
"title": title,
"FormServicer": FormServicer,
"Sdescription_formset": Sdescription_formset,
"Stag_formset": Stag_formset, #new
}
return render(request, 'serv.html', context)
Models.py:
class Servicer(models.Model):
servicer_name = models.CharField(max_length=45,unique=True)
pub_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.servicer_name
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
# adds view site button on admin.
def get_absolute_url(self):
return '/video/%s/' % self.id
class Sdescription(models.Model):
servicer = models.ForeignKey(Servicer, on_delete=models.CASCADE, blank=True, null=True)
address = models.CharField(max_length=100,blank=True, null=True, unique=True)
address_line_2= models.CharField(max_length=100, blank=True, null=True)
city= models.CharField(max_length=50, blank=True, null=True)
state= models.CharField(max_length=25, blank=True, null=True)
zip = models.CharField(max_length=15, blank=True, null=True)
near_landmark = models.CharField(max_length=200, blank=True, null=True)
main_contact_name = models.CharField(max_length=20, blank=True, null=True)
phone_number1 = models.CharField(max_length=20, blank=True, null=True)
phone_number2 = models.CharField(max_length=20, blank=True, null=True)
comments = models.TextField(max_length=200, blank=True, null=True)
website = models.CharField(max_length=40, blank=True, null=True)
email = models.EmailField(max_length = 20,blank=True, null=True)
image = models.ImageField(upload_to='uploads/',blank=True, null=True)
def __str__(self):
return '%s %s %s %s %s %s %s %s %s %s %s' % (self.address,
self.address_line_2, self.city, self.state, self.zip,
self.near_landmark, self.phone_number1, self.phone_number2, self.comments,
self.website, self.email)
class Stag(models.Model):
tag_name = models.CharField(max_length=30,blank=True, null=True)
price = models.DecimalField(max_digits=9, decimal_places=2,blank=True,
null=True)
comments = models.CharField(max_length=100,blank=True, null=True)
sdescription = models.ForeignKey(Sdescription,
on_delete=models.CASCADE,blank=True, null=True)
def __str__(self):
return '%s $%s %s' % (self.tag_name, self.price, self.comments)
Forms.py:
class InputFormServicer(forms.ModelForm):
class Meta:
model = Servicer
fields=['servicer_name']
class InputFormSdescription(forms.ModelForm):
class Meta:
model = Sdescription
fields=['address','address_line_2','city','state','zip','near_landmark',
'main_contact_name', 'phone_number1', 'phone_number2', 'comments',
'website', 'email','image'
]
class InputFormStag(forms.ModelForm):
class Meta:
model = Stag
fields=['tag_name','price','comments']
Serv.html:
<h1>{{title}}</h1>
<form method='POST' action=''> {% csrf_token %}
{{FormServicer}}
{{Sdescription_formset.as_p}}
{{Sdescription_formset.management_form}}
{{Stag_formset.as_p}}
{{Stag_formset.management_form}}
<input type='submit' value='save'/>
</form>