Hello! This is my first question, so please excuse any mistakes.
I'm using django 1.8 (working on migration to 2.0).
I need to implement a one-to-one generic relation. To illustrate suppose I have three classes, the first representing and abstract class in a store and the other specific products which need extra information:
AbstractProduct(models.Model):
<fields here>
sale = GenericRelation('Sales')
class Meta:
abstract = True
Books(AbstractProduct):
<fields here>
Magazines(AbstractProduct):
<fields here>
class Sales(models.Model):
<fields here>
content_type = models.ForeignKey(...)
object_id = models.PositiveIntegerField(...)
content_object = GenericForeignKey('content_type', 'object_id')
My problem is that by using this approach whenever I do
>>> Books.objects.get(pk=1).sale
I get back a manager since django does not know it is a one-to-one relation. And it makes me think that maybe I'm approaching this in the wrong way.
My question is:
(a) is this design a good solution for this problem? Is there a better approach? Has anyone solved a similar problem in a better way?
(b) is there a way to create a generic One-To-One relation?
I'm open to answers pointing to good blogs/books/articles which give a more formal approach to this kind of design issue, if you know a good one.
Thanks in advance! Yours,
Vitor.