CreateView
def post(self, request, *args, **kwargs):
form = BusinessDetailForm(request.POST,request.FILES or None)
form2 = MultipleImageForm(request.POST or None, request.FILES or None)
files = request.FILES.getlist('images')
if all([form.is_valid(),form2.is_valid()]):
forms = form.save(commit=False)
geolocator = Nominatim()
location = geolocator.geocode(self.request.POST.get("pin_code",False))
forms.latitude = location.latitude
forms.longitude = location.longitude
forms.created_by = self.request.user
forms.themes = self
# forms.object_id = int(self.request.POST.get("id",False))
forms.save()
models.py
class BusinessDetail(models.Model):
# content_type = models.ForeignKey(ContentType,on_delete = models.CASCADE)
# object_id = models.PositiveIntegerField()
# content_object = GenericForeignKey('content_type','object_id')
themes = models.ForeignKey(Themes,on_delete=models.CASCADE,primary_key=False)
# object_id = models.PositiveIntegerField()
created_by =models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,primary_key=False)
listin_catagory =models.CharField(max_length=200,choices=listin_Catagory,blank=True)
business_name =models.CharField(max_length=200,blank=True)
#themes table
class Themes(models.Model):
theme_created_by =models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,primary_key=False)
default_theme = models.BooleanField(blank=True,default=False)
technology_theme = models.BooleanField(blank=True,default=False)
错误是
无法分配“”:“ BusinessDetail.themes”必须是“ Themes”实例。
答案 0 :(得分:2)
在CreateView
发布方法中,您需要将主题对象分配给forms.themes
。
像
forms.themes=Themes.objects.first()
但是您可以动态分配主题。喜欢
forms.themes=Themes.objects.filter(pk=somevalue).last()