[Django] #33341: The PrimaryKey of an Unmanaged Model is incorrectly regognized (accessing database VIEW)

5 views
Skip to first unread message

Django

unread,
Dec 4, 2021, 2:13:57 PM12/4/21
to django-...@googlegroups.com
#33341: The PrimaryKey of an Unmanaged Model is incorrectly regognized (accessing
database VIEW)
-------------------------------------+-------------------------------------
Reporter: Hukkinen | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.2
Uncategorized | Keywords: Unmanaged model,
Severity: Normal | Database VIEW, postgres
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have used the following method earlier successfully with Django 3.0.6,
but now there's a bug.

But now the 3.2.9 catches a "Gotcha" in django/db/models/fields/related.py
in "get_instance_value_for_fields" (line 662), I believe incorrectly.

Currently I have:

– A view on postgres (to access a managed model "Rules", with default id
as PK): CREATE OR REPLACE VIEW rules_view

– Unmanaged model to access "Rules" data via "rules_view" view like this:

{{{
class LinkedRules(Rules)
....
class Meta:
managed = False
db_table = 'rules_view

}}}


The problem: I am able to retrieve the data with, only with a raw SQL
query, after changing the field name from id to rules_ptr_id like this:


{{{
LinkedViewRules.objects.raw(f'SELECT id as rules_ptr_id , ...
}}}


Hope this helps.

PS. Couldn't test my current project with old <3.2 version, due to other
incompatibilities (with numpy array withespaces).
(Table names are paraphrased, hopefully without errors)


Hint from django/db/models/fields/related.py:

# Gotcha: in some cases (like fixture loading) a model can
have
# different values in parent_ptr_id and parent's id. So, use
# instance.pk (that is, parent_ptr_id) when asked for
instance.id.

--
Ticket URL: <https://code.djangoproject.com/ticket/33341>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 6, 2021, 4:50:04 AM12/6/21
to django-...@googlegroups.com
#33341: The PrimaryKey of an Unmanaged Model is incorrectly regognized (accessing
database VIEW)
-------------------------------------+-------------------------------------
Reporter: Janne | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 3.2
Severity: Normal | Resolution: needsinfo
Keywords: Unmanaged model, | Triage Stage:
Database VIEW, postgres | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


Comment:

Hi. I don't think there's quite enough detail here to see what you're
issue is.

In general, using `managed=False` works with views, and has for many
years, see e.g. #16320.

You mention the primary key, but don't provide the model definition.
Ensure you're declaring a field as `primary_key=True`.
There was a recent [https://pganalyze.com/blog/postgresql-views-django-
python walk through on using views with Django (targeting Postgres)] that
might help you if you walk through it.

Otherwise, it looks like a usage issue, so you'll need to use one of the
support channels. See TicketClosingReasons/UseSupportChannels.

If you can provide a sample project or a failing test case showing a fault
in Django, then very happy to re-open and have a look.
Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/33341#comment:1>

Reply all
Reply to author
Forward
0 new messages