Opened 4 years ago

Closed 4 years ago

#30756 closed Bug (duplicate)

ModelForm does not work well with the override field on Inherited Model

Reported by: Ramon Soares Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following the samples:

class City(models.Model):
    name = models.CharField(max_length=100)


class AddressBase(models.Model):
    city = models.ForeignKey(City, on_delete=models.PROTECT)
    ...
    country = models.ForeignKey(...)
    
    class Meta:
        abstract = True


class Address(AddressBase):
    city = models.CharField(max_length=100)


class AddessForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ("city", )

I expect a rendered form with the city field as a CharField, but I got the following error:

Cannot assign "''": "Address.city" must be a "City" instance.

The problem, I think, is with the code above, on models.Field.contribute_to_class

        if self.column:
            # Don't override classmethods with the descriptor. This means that
            # if you have a classmethod and a field with the same name, then
            # such fields can't be deferred (we don't have a check for this).
            if not getattr(cls, self.attname, None):
                setattr(cls, self.attname, DeferredAttribute(self.attname))
}}

Change History (1)

comment:1 Changed 4 years ago by Carlton Gibson

Resolution: duplicate
Status: newclosed

HI Ramon, this looks like a Duplicate of #30427. (There is a patch for that you could try. Feedback on the ticket or PR welcome.)

Note: See TracTickets for help on using tickets.
Back to Top