How to do a generic field in an app.

22 views
Skip to first unread message

Jonty Needham

unread,
Sep 19, 2016, 12:30:01 PM9/19/16
to django...@googlegroups.com
I want to have a model that has a foreignkey to a model from another app. The app can be one of many.

So far I've come up with a two field solution with:

model_type = ForeignKey(ContentType)
pk_field = PositiveIntegerField()


with an appropriate getter. Is there a better way to do this? It feels like this has already been solved in a known "good" way.

Tim Graham

unread,
Sep 19, 2016, 1:48:37 PM9/19/16
to Django users

Jonty Needham

unread,
Sep 20, 2016, 5:55:06 AM9/20/16
to django...@googlegroups.com
Awesome -- thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a81b5dee-3a80-4a05-a3c1-d82b92d0395a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonty Needham

unread,
Sep 20, 2016, 10:01:53 AM9/20/16
to django...@googlegroups.com
So now, I'd like to do the following:

class AppConfig(models.Model):
    my_content_type = ForeighnKey(ContentType)
    <other_important_fields)

class Submission(models.Model):
    object_id = PositiveIntegerField()
    my_config = ForiegnKey(AppConfig)
    content_object = GenericForeignKey("my_config__my_content_type", "object_id")

But Django says the field cannot be found. I want the AppConfig structure separated from the actual Submission, but is there a way of doing this?

Jonty
Reply all
Reply to author
Forward
0 new messages