django, contenttype and m2m generic relations

411 views Asked by At

I have the following models:

  1. Application
    1. Contact Form
    2. Game
    3. Landing Page
  2. Client

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?

1

There are 1 answers

0
tutuDajuju On

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.