Django: Inheritance Model: FK & M2M

I will give, trying to do this: http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

Using this style

This is saved as common / abstract.py

class OtherModel(models.Model):

   something = Charfield(max_length=100)

   class Meta:
            abstract = True

class Base(models.Model):
        fk_model = models.ForeignKey(OtherModel, related_name="%(app_label)s_%(class)s_related")

        class Meta:
            abstract = True

Then I import it into another file. Call this application /models.py

from common.abstract import Base

class ChildB(Base):
    pass

I installed the "application", but not the "general". It will import beautifully without FK or M2M relationships, but when I try to add it, I get this error:

/lib/python2.6/site-packages/django/db/models/fields/related.py ", 808, init  assert not to.meta.abstract," % s % s"% (self. class._ name__, to._meta.object_name) AssertionError: ForeignKey OtherModel

, ... , - , . , , , , , .

+3
2

.....

:

from django.db import models

class CommonInfo(models.Model):

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

: AssertionError: ForeignKey OtherModel. , FK M2M , .

from django.db import models

class CommonInfo(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

. . , - .

+1

, , (ManyToManyField to OtherModel). , abstract = True OtherModel common INSTALLED_APPS!

OtherModel Base, , !

+2

Source: https://habr.com/ru/post/1770023/


All Articles