0

I.e. we have SomeSeries with several SomeDecors, where ForeignKey of SomeDecor points to SomeSeries. I want both to be abstract and later instantiate several pairs of it (with it's own tables in db). Is it possible? I.e.

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

    vendor = models.ForeignKey(Vendor)
    name = models.CharField(max_length=255, default='')
    def __unicode__(self):
        return "{} {}".format(self.vendor, self.name)


class SomeDecor(WithFileFields):
    class Meta:
        abstract = True

    series = models.ForeignKey(SomeSeries) # some magic here to make ForeignKey to abstract model
    texture = models.ImageField()
# -------------------------------------------
class PlinthSeries(SomeSeries): pass
class PlinthDecor(SomeDecor): pass
# Some magic to make PlinthDecor.series points to PlinthSeries  

EDIT Actually I don't want complicity of polymorphic relations, I want pure abstract models just to save typing (what abstract models are initially for). Suppose in my case the simplest way is to exclude ForeignKey from base model and type it only in all inherited models:

class SomeSeries(models.Model):
    class Meta:
        abstract = True
    #...

class SomeDecor(WithFileFields):
    class Meta:
        abstract = True

    series = None #?
    #..
    texture = models.ImageField()

    def do_anything_with_series(self): pass

class PlinthSeries(SomeSeries): pass
class PlinthDecor(SomeDecor): pass
    series = models.ForeignKey(PlinthSeries)
john.don83
  • 103
  • 1
  • 10

2 Answers2

2

You can't create ForeignKey referencing abstract model. It's, even, doesn't make any sense, because ForeignKey translates into Foreign Key Constraint which have to reference existing table.

As a workaround, you can create GenericForeignKey field.

Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
0

You can not do it because if you create two class inherit from your abstract class to what class your foreignkey should do? for first or for second? So you need to create GenericForeignKey or not do any field and only after create model inherits from your abstract model add your foreign key.

Andrei Berenda
  • 1,946
  • 2
  • 13
  • 27