Issue
i want a field be OneToOneField to an abstract object, the abstract will be never used, instead will be used subclasses:
from django.db import models
class Abs(models.Model):
useType = True
absName = models.CharField(max_length=10, default='mickey', blank=True)
class Meta:
abstract = True
from django.db import models
from .abs import Abs
class Sub(Abs):
useType = False
name=models.CharField(max_length=10, default='', blank=True)
from django.db import models
from .abs import Abs
class ParentClass(models.Model):
abs = models.OneToOneField(Abs, on_delete=models.CASCADE)
but when make makemigrations i receive following error:
books.ParentClass.abs: (fields.E300) Field defines a relation with model 'Abs', which is either not installed, or is abstract.
books.ParentClass.abs: (fields.E307) The field books.ParentClass.abs was declared with a lazy reference to 'books.abs', but app 'books' doesn't provide model 'abs'.
If Abs was a real object all works fine, but Abs should never be a real object, so i thought should be abstract, it's only a basket to common info/methods, an interface.
Is there a way to do that?
Many thanks
Solution
You cannot have any reference field(OneToOne, ManyToOne, ForeignKey) to an abstract class 'cause they have not a corresponding table in the database.
Keep the code as you have it but suppress the:
class Meta:
abstract = True
and it should be fine.
Your database will have an Abs table with all the Abs objects and their general attributes, and the specifications will hold the same objects with the extra attributes.
Answered By - 42WaysToAnswerThat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.