I have the following models:
After I add a new Client, I want to add new applications under client. For this, I added a m2m field under Clients to Applications like this:
applications = models.ManyToManyField(Application, verbose_name=u'Applications')
Since an application can be anything, I found out that I needed to use contenttype framework. So I placed the following under Application model:
applicationContentType = models.ForeignKey(ContentType)
applicationId = models.PositiveIntegerField()
application = generic.GenericForeignKey('applicationContentType', 'applicationId')
This way I can add new applications by selecting content type (content form in this case) and typing existing Contact Form's id. And I can see it in Client's m2m field on admin page.
However when I do the following, I can't see the application I just added to the Client:
In [2]: t = Client.objects.get()
In [3]: t.applications.all()
Out[3]: []
And having to remember the newly added Contact Form's id is not very nice. Is there an elegant solution of this? Or should I change my point of view to the problem and do it in a different way?
Note: I know this is a very old question, but thought someone might benifit this
What you are describing would work perfectly if the each of the application models (or their abstract
parent ) would have a m2m key to the client. I would not recommend using generic foreign keys unless it's absolutely inevitable (trust me, you'll thank me later).
For instance, take an application model:
class Application(models.Model):
class Meta:
abstract = True
field1 = ...
field2 = ...
client = models.ManyToManyField('clients.Client',
related_name="related_%(class)s")
Using this approach you will have the reverse fields available to the client via the related_games
, related_contactforms
and related_landingpages
respectively.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.