join on plain integerfield

64 views
Skip to first unread message

Marek Rouchal

unread,
Nov 21, 2022, 10:22:52 AM11/21/22
to Django users

I need to access an existing database whose schema I cannot alter; I used the Django tool to extract the tables to create models.py, which worked great, and I already hacked some model definitions, e.g. to replace a simple IntegerField with a ForeignKey. But there are specific tables, where there is an IntegerField which may contain a ForeignKey from more than one table (think of a group and a user table; whether the reference is a user or a group, is defined by another column):

class Permission(models.Model):
    ...
    user_or_group_id = models.IntegerField(db_column='user_or_group_id', blank=True, null=True)
    is_group = models.IntegerField(db_column='is_group', blank=True, null=True)

So what I would need here is something like a union on ForeignKey (coming from either a User or a Group table) in the model, but I guess that does not exist?
So I used raw SQL for the required joins; I have not been able to formulate a Django queryset to join on a field which is not modeled with ForeignKey.
That is why I was asking for some better support of raw SQL, reference is this ticket: https://code.djangoproject.com/ticket/34168

Any suggestion how I can model such an integerfield, so that I can do joins on it using the Django ORM queryset syntax?

Ryan Nowakowski

unread,
Nov 21, 2022, 2:41:01 PM11/21/22
to django...@googlegroups.com
That's sort of how generic relations[1] work. You might be able to make it fit.

[1] https://docs.djangoproject.com/en/4.1/ref/contrib/contenttypes/#generic-relations

Marek Rouchal

unread,
Nov 25, 2022, 1:44:59 AM11/25/22
to django...@googlegroups.com
Thanks Ryan, that looks promising. I have to dig deeper, but upon first read I saw that an additional field would be required in the model definition; question: would that also require an additional column in the database (which I could not afford), or is it purely a „meta“ kind of field for ORM purposes only?


Ryan Nowakowski <ry...@fattuba.com> schrieb am Mo. 21. Nov. 2022 um 20:41:
That's sort of how generic relations[1] work. You might be able to make it fit.

[1] https://docs.djangoproject.com/en/4.1/ref/contrib/contenttypes/#generic-relations

-- 
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/1YTjSHaj0x0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/C5E28F70-7521-4CFF-A43E-1B5576D144CF%40fattuba.com.

Ryan Nowakowski

unread,
Nov 26, 2022, 8:48:20 AM11/26/22
to django...@googlegroups.com
It only uses 2 columns, the "type" of the relation and the id of the
related object. That's why I thought it might be a good fit for your
use case. If you can't use it as-is because your "type" column is a
boolean instead of a foreignkey to ContentType, you can look at the
GenericForeignKey code[1] to see if you can copy and adapt it.

[1] https://github.com/django/django/blob/main/django/contrib/contenttypes/fields.py#L25
> > <https://groups.google.com/d/msgid/django-users/C5E28F70-7521-4CFF-A43E-1B5576D144CF%40fattuba.com?utm_medium=email&utm_source=footer>
> > .
> >
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Bb80mBJqZWsBg_QiTY7O-0dzxcCXEhYv6-PQQ_ukV895bs%2Bww%40mail.gmail.com.

Clive Bruton

unread,
Dec 8, 2022, 10:33:05 PM12/8/22
to django...@googlegroups.com
Try Django Relativity:

https://pypi.org/project/django-relativity/


-- Clive
Reply all
Reply to author
Forward
0 new messages