Reputation: 3611
I have three different models (Job, Tender, News).
class Tender(models.Model):
title = models.CharField(max_length=256)
description = models.TextField()
department = models.CharField(max_length=50)
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)
class Job(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=256)
qualification = models.CharField(max_length=256)
employer = models.CharField(max_length=50)
salary = models.IntegerField()
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)
class News(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=150)
body = models.TextField()
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)
Now I would like to make another model ( class Post), so that whenever a new object is created (job or tender or news), a new post object should be created, basically this will be done using signals.
Now with this post class
I am very new to django's content type, and confused what fields will be required. I only know for sure that this three fields are required, content type
, object id
and content object
.
class Post(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
But to get the functionality that I want (i.e. filter and order by), do I have to add user
, tags
and pub_date
field to the Post
class?
Upvotes: 0
Views: 164
Reputation: 5793
From Django documentation:
Due to the way GenericForeignKey is implemented, you cannot use such fields directly with filters (filter() and exclude(), for example) via the database API. Because a GenericForeignKey isn’t a normal field object, these examples will not work:
I believe the short answer is you will need to include user
, tags
and pub_date
or perhaps better would be links to the models through ForeignKey.
Upvotes: 0