1

I have a model that I'm using as a reference point to create another model:

class ImYourFather(models.Model):
    force = fields.HTMLField(null=True, blank=True)
    supermarket_planets = models.HTMLField(null=True, blank=True)
    destroying_planets = models.HTMLField()

class Luke(ImYourFather):
    # Inheriting all the fields from father
    the_cool_kid = models.HTMLField() # extra field added

I DO NOT want to inherit the destroying_planets field, Is this possible?
I'm asking specifically because destroying_planets is supposed to be mandatory in the father model but I'd like to have it optional in the child model.
Is this achievable in another way?

2
  • If you inherit from a class you will inherit all the fields. Also check this: docs.djangoproject.com/en/dev/topics/db/models/… Commented Feb 5, 2014 at 12:22
  • Thanks, Is there anyway to tells Django that a mandatory field in the father should be optional in the child (without modifying the father)? Commented Feb 5, 2014 at 12:26

1 Answer 1

2

You can't partially inherit but you can manually create the models with whatever fields you need. This is not exactly the same as multi-table inheritance but that is the only way to partially inherit fields:

class Base(models.Model):
    force = fields.HTMLField(null=True, blank=True)
    supermarket_planets = models.HTMLField(null=True, blank=True)

    class Meta(object):
        abstract = True

class ImYourFather(Base):
    destroying_planets = models.HTMLField()

class Luke(Base):
    # optional in case you need this
    father = models.OneToOneField(ImYourFather, related_name='lukes')
    the_cool_kid = models.HTMLField() # extra field added

edit

Another approach is to simply copy the fields from the father. This is all untested so Django might bark at you for some of it). The advantage is that no monkey-patching but should work:

exclude = ['id', 'destroying_planets']
try: # Django 1.7
    fields = {i.attname: i.clone() for i in ImYourFather._meta.fields if not i.attname in exclude}
except AttributeError: # Django < 1.7
    fields = {i.attname: deepcopy(i) for i in ImYourFather._meta.fields if not i.attname in exclude}
Base = type('Base', (models.Model,), fields)

class Luke(Base):
    # optional in case you need this
    father = models.OneToOneField(ImYourFather, related_name='lukes')
    the_cool_kid = models.HTMLField() # extra field added
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but I don't have the option to modify the father class. Is there anyway to tells Django that a mandatory field in the father should be optional in the child?
Well... you can modify the father class if you want so much ( Hint: monkey patching ) - but that will change the parent also! If you want I can provide more details.
That would be useful, tell us more if you can :)
I added another approach without monkey-patching. its untested but should give you an idea.
Thansk, I'll be back on this when I'll try it and get some errors from Django :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.