Changes between Initial Version and Version 2 of Ticket #27560


Ignore:
Timestamp:
Dec 7, 2016, 10:44:16 AM (8 years ago)
Author:
Lorenzo Peña
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #27560

    • Property Summary Formset.save for model with foreign key to concrete base modelFormset.save() crashes for model with foreign key to concrete base model
    • Property Type UncategorizedBug
  • Ticket #27560 – Description

    initial v2  
     1Model hierharchy
    12
    23{{{
    3 class A(models.Model):
    4   pass
     4class ConcreteBase(models.Model):
     5    ...
    56
    6 class B(A):
    7   pass
    87
    9 class C(models.Model):
    10   some_model = models.ForeignKey(SomeModel)
    11   a_link = models.ForeignKey(A)
     8class Descendant(ConcreteBase):
     9    ...
    1210}}}
    1311
    14 I'm using an inlineformset_factory where the parent model is SomeModel and the child model is C.
    15 When calling formset.save() I'm getting an error from django/forms/models.py (line 910)
     12Master / Detail type models (e.g. Order and OrderDetail)
     13
     14{{{
     15class MasterModel(models.Model):
     16    ...
     17
     18
     19class DetailModel(models.Model):
     20   
     21    master = models.ForeignKey(MasterModel, ...)
     22    concrete_base = models.ForeignKey(ConcreteBase, ...)
     23}}}
     24
     25Form definition
     26
     27{{{
     28class MasterModelForm(forms.ModelForm):
     29    ...
     30
     31
     32class DetailModelForm(forms.ModelForm):
     33    ...
     34    # master excluded from fields as will be manually added in view
     35    ...
     36}}}
     37
     38Formset definition
     39
     40{{{
     41DetailModelFormset = inlineformset_factory(MasterModel, DetailModel, form=DetailModelForm)
     42}}}
     43
     44View definition
     45
     46{{{
     47from django.views.generic.edit import CreateView #### <---- the error only happens on create view of MasterModel
     48
     49class MasterModelAdd(CreateView):
     50    model = MasterModel
     51    form_class = MasterModelForm
     52    ...
     53
     54    def form_valid(self, form):
     55        master_instance = form.save()
     56        detail_formset = DetailModelFormset()(self.request.POST)
     57        detail_instances = detail_formset.save(commit=False) ### <------ this line raises the error
     58        for detail_instance in detail_instances: ### <----- it never gets here
     59            detail_instance.master = master_instance
     60            detail_instance.save()
     61
     62}}}
     63
     64When calling formset.save() I'm getting an error from django/forms/models.py (line 910) regardless of the value of commit passed.
    1665
    1766{{{
     
    2271The getattr call fails
    2372
    24 if I wrap both lines in: if hasattr(self.instance, self.fk.remote_field.field_name):
     73if I wrap both lines in: {{{ if hasattr(self.instance, self.fk.remote_field.field_name): }}}
    2574it works good.
    2675
Back to Top