A
A
Artur Zenkov2015-10-14 01:10:02
Python
Artur Zenkov, 2015-10-14 01:10:02

How to redefine a property in the heir?

Can anyone explain how this is resolved?
Why does saving work in the child method, but the value of the parent remains in the content property?
And is it possible to somehow override the property?

from django.db import models

class Father(models.Model):
    COST = 100
    content = models.TextField(max_length=COST)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if self.COST > 200:
            self.action = True
        super(Father, self).save(*args, **kwargs)

class Children(Father):
    COST = 500
    action = models.BooleanField(default=False)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bromzh, 2015-10-14
@bromzh

try declaring the action field in the parent class:

class Father(models.Model):
    COST = 100
    content = models.TextField(max_length=COST)
    action = models.BooleanField(default=False)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if self.COST > 200:
            self.action = True
        super(Father, self).save(*args, **kwargs)

class Children(Father):
    COST = 500

V
Vladislav Sklyar, 2015-10-14
@VladSkliar

Try:

class Children(Father):
    COST = 500
    action = models.BooleanField(default=False)

    class Meta:
        content = models.TextField(max_length=COST)

or
class Father(models.Model):
    COST = 100
    content = models.TextField(max_length=self.COST)
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question