How can I filter these Django records? -
I have a set of dijongo models as shown in the following diagram (reverse relationships names shown in yellow bubbles ):
In each respect, there can be 0 or more items in a person
.
Additionally, the slug
field (unfortunately) is not unique; Multiple person
records can have the same slug field originally, these records are duplicates.
I want to get a list of all records that meet the following criteria: At least one all duplicate records with the entry (which is the same slug)> or at least one
Audio
or at least one episode
or at least one article
.
So far, I have the following query:
Person.objects.values ('slug'). Anotate (num_records = count ('slug')). Filter (num_records__gt = 1)
This group records all by slag
, then adds a num_records
attribute that says How many records are calculated, but no additional filtering is done (and I do not even know that this will work right anyway, because there is a set of duplicate records, some may be, for example, and entry
and the other can have an article
). In short, I want to find all duplicate records and want them to be in a record, along with their respective models.
What is the best way to do this with Django?
I will do this in many questions. First of all you have a list of duplicates that you have:
dupes = [p ['slug'] for Person.objects.values ('slug'). Anatet (num_records = count ('slug')). Filter (num_records__gt = 1)]
Then I loop through them, and I want to decide for each one (to make arbitrary decisions - choose one first). After that, for all other primary keys, just update all other objects to indicate the primary key you've chosen:
slog in duplicts: pks = [p.id for Person in p .objects.filter (slug = slug)] PK in PK [1:]: Audio.obies.filter (person = pk). Update (person = px [0]) author. Biesesfilter (person = pk). Update (person = PX [0]) Episode.Objectsfilter (person = PK). Update (person = PX [0]) Entry.objects.filter (person = PK) .update (person = px [0])
Comments
Post a Comment