'generic_inlineformset_factory' 产生“'NoneType' 对象没有属性‘_meta’”
'generic_inlineformset_factory' produces " 'NoneType' object has no attribute '_meta' "
我无法使用 generic_inlineformset_factory
保存我的前端选择。
如果我在前端没有 select 任何东西,行 sports = sports_formset.save(commit=False)
不会产生任何错误,但是如果我选择任何东西,我会得到 'NoneType' object has no attribute '_meta'
代码底部的# PROBLEM PART
# models
class M2MProfilesToSportTypeGroups(models.Model):
MASTERY_CHOICES = (
(1, _('newby')),
(2, _('amateur')),
(3, _('semi-pro')),
(4, _('pro'))
)
mastery = models.IntegerField(_('mastery'), null=True, blank=True, choices=MASTERY_CHOICES)
sport_type_group = models.ForeignKey(SportTypesGroups, verbose_name=_('sport type group'))
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
# views
class CompanyProfileCreateView(LoginRequiredMixin, TemplateView):
template_name = 'profiles/create_n_update.html'
def get(self, request, *args, **kwargs):
profile_form = CompanyProfileForm() #show_criterias=False)
PlaceTypeFormSet = inlineformset_factory(CompanyProfiles, M2MCompanyProfilesToPlaceTypeGroups,
fields='__all__', can_delete=False)
SportTypeFormSet = generic_inlineformset_factory(M2MProfilesToSportTypeGroups,
can_delete=False)
places_formset = PlaceTypeFormSet()
sports_formset = SportTypeFormSet()
return self.render_to_response({'form': profile_form,
'places_formset': places_formset,
'sports_formset': sports_formset})
def post(self, request, *args, **kwargs):
profile_form = CompanyProfileForm(data=request.POST)
PlaceTypeFormSet = inlineformset_factory(CompanyProfiles, M2MCompanyProfilesToPlaceTypeGroups,
fields='__all__', can_delete=False)
SportTypeFormSet = generic_inlineformset_factory(M2MProfilesToSportTypeGroups,
can_delete=False)
places_formset = PlaceTypeFormSet(data=request.POST)
sports_formset = SportTypeFormSet(data=request.POST)
if profile_form.is_valid() and places_formset.is_valid() and sports_formset.is_valid():
profile_form.instance.created_by = request.user
company_profile = profile_form.save()
places = places_formset.save(commit=False)
for place in places:
place.company_profile = company_profile
place.save()
# PROBLEM PART
sports = sports_formset.save(commit=False)
for sport in sports:
obj.content_object = company_profile
obj.save()
return redirect(company_profile)
return self.render_to_response({'form': profile_form,
'places_formset': places_formset,
'sports_formset': sports_formset})
感谢任何建议。
找到解决方案。
# PROBLEM PART
的变化:
sports_formset = SportTypeFormSet(data=request.POST, instance=company_profile)
sports_formset.save()
提供 company_profile
我们让 Django 找出使用哪种 content_type
和 object_id
。
我无法使用 generic_inlineformset_factory
保存我的前端选择。
如果我在前端没有 select 任何东西,行 sports = sports_formset.save(commit=False)
不会产生任何错误,但是如果我选择任何东西,我会得到 'NoneType' object has no attribute '_meta'
代码底部的# PROBLEM PART
# models
class M2MProfilesToSportTypeGroups(models.Model):
MASTERY_CHOICES = (
(1, _('newby')),
(2, _('amateur')),
(3, _('semi-pro')),
(4, _('pro'))
)
mastery = models.IntegerField(_('mastery'), null=True, blank=True, choices=MASTERY_CHOICES)
sport_type_group = models.ForeignKey(SportTypesGroups, verbose_name=_('sport type group'))
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
# views
class CompanyProfileCreateView(LoginRequiredMixin, TemplateView):
template_name = 'profiles/create_n_update.html'
def get(self, request, *args, **kwargs):
profile_form = CompanyProfileForm() #show_criterias=False)
PlaceTypeFormSet = inlineformset_factory(CompanyProfiles, M2MCompanyProfilesToPlaceTypeGroups,
fields='__all__', can_delete=False)
SportTypeFormSet = generic_inlineformset_factory(M2MProfilesToSportTypeGroups,
can_delete=False)
places_formset = PlaceTypeFormSet()
sports_formset = SportTypeFormSet()
return self.render_to_response({'form': profile_form,
'places_formset': places_formset,
'sports_formset': sports_formset})
def post(self, request, *args, **kwargs):
profile_form = CompanyProfileForm(data=request.POST)
PlaceTypeFormSet = inlineformset_factory(CompanyProfiles, M2MCompanyProfilesToPlaceTypeGroups,
fields='__all__', can_delete=False)
SportTypeFormSet = generic_inlineformset_factory(M2MProfilesToSportTypeGroups,
can_delete=False)
places_formset = PlaceTypeFormSet(data=request.POST)
sports_formset = SportTypeFormSet(data=request.POST)
if profile_form.is_valid() and places_formset.is_valid() and sports_formset.is_valid():
profile_form.instance.created_by = request.user
company_profile = profile_form.save()
places = places_formset.save(commit=False)
for place in places:
place.company_profile = company_profile
place.save()
# PROBLEM PART
sports = sports_formset.save(commit=False)
for sport in sports:
obj.content_object = company_profile
obj.save()
return redirect(company_profile)
return self.render_to_response({'form': profile_form,
'places_formset': places_formset,
'sports_formset': sports_formset})
感谢任何建议。
找到解决方案。
# PROBLEM PART
的变化:
sports_formset = SportTypeFormSet(data=request.POST, instance=company_profile)
sports_formset.save()
提供 company_profile
我们让 Django 找出使用哪种 content_type
和 object_id
。