Good day!
New to django, but familiar with the principles involved. Here is my
situation. (sorry if long winded...)
I have an entry and categories (called divisions) in a many-to-many
relationship. However, each entry has a main division. So far the
model looks like this:
class Division(models.Model):
name = models.CharField(maxlength=100)
identifier = models.SlugField(maxlength=30, db_index=True,
unique=True)
class Entry(models.Model):
name = models.CharField(maxlength=255)
mainDivision = models.ForeignKey(Division,
related_name='entryMain_set')
divisions = models.ManyToManyField(Division, blank=True)
Conceptually, the mainDivision is treated as one of the divisions in
most cases; i.e. given a division, I want all entries connected
through either relationship. But in terms of modeling, I have two
options: either the mainDivision is included in the the "divisions"
set or not. (Any best practice?)
My initial choice was not, so as to avoid data duplication, and use
complex queries.
For example, I can do this and get an accurate answer:
id=1
Entry.objects.filter(Q(mainDivision__identifier = id)|
Q(divisions__identifier = id))
Now, suppose that all I have for now are mainDivisions, i.e. the
entry_divisions table is empty.
And suppose I am retrieving by identifier rather than id...
id='someIdentifier'
Entry.objects.filter(Q(divisions__identifier = id))
=> []
which is accurate since the divisions relationship is empty;
Entry.objects.filter(Q(mainDivision__identifier = id))
=> [<Entry: 1>, <Entry: 3>,...]
which is also accurate;
However,
Entry.objects.filter(Q(mainDivision__identifier = id)|
Q(divisions__identifier = id))
=> []
which does not agree with the semantics of 'OR'. So that is the first
bug report (admittedly a corner case.)
(I suspect it's an issue on INNER vs OUTER JOIN. Is there an easy way
to look at the underlying SQL query)?
Note: even with id, I also get some interesting behaviours
So I started on the track and started to enforce the mainDivision
being in the divisions.
(That was after I ran into the identifier bug, but before I realized
my complex query worked with id...)
My first attempt was to add the following function in Entry:
def save(self):
self.divisions.add(self.mainDivision)
super(Entry, self).save()
(I first checked that 'add' could be applied many times without
creating duplicates.)
Of course, I ran in trouble with new objects, which do not have an Id
yet, so I refined thus:
def save(self):
if not self.id:
super(Entry, self).save() # to get the Id
self.divisions.add(self.mainDivision)
super(Entry, self).save() # annoying double save...
If I apply 'save' to all instances of Entry in the shell, the
relationship is added appropriately.
However, if I modify or create an entry in the web admin interface,
the relationship does not show up in divisions. I know 'save' is
called (because I get stack traces if I raise something there), but
the 'add' seems to fail in that case.
Any clue? I am not sure this is a bug, but it sure puzzles me...
Cheers,
Marc-Antoine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---