davidalk
davidalk

Reputation: 105

Django Model inheritance error "field ... clashes with the field"

I'm having a problem when using multi-table inheritance in Django and I didn't find something that solved it.

I have these two models:

class Person(models.Model):
    id = models.CharField(primary_key=True, max_length=12, default="")
    name = models.CharField(max_length=12, default="")
    birthday = models.DateField()

class Parent(Person):
    work = models.CharField(max_length=70, default="")
    spouce_field = models.OneToOneField(Person, on_delete=DO_NOTHING, related_name="spouce_field")

And I get this error when running python3 manage.py makemigrations:

ERRORS:

family.Parent.spouce_field: (models.E006) The field 'spouce_field' clashes with the field 'spouce_field' from model 'person.person'.

Any idea what am I doing wrong?

Upvotes: 1

Views: 1634

Answers (1)

ASG
ASG

Reputation: 29

I believe mrcai have answered your question here:

Mark your class Person as an abstract class to avoid field clashing.

You can specify Profile is an abstract class. This will stop the check from being confused with your parent fields.

class Meta:
   abstract = True

Upvotes: 2

Related Questions