Short version: during a Save from the Admin interface, m2m
associations computed and applied as a side effect of calling save()
do not seem to be recorded (permanently?) in the database. The same
call from a view or a standalone script works.

Longer version:

I have the following class:

==============================
class Vineyard(models.Model):
  ...
    varietals = models.ManyToManyField(Varietal, blank=True,
null=True)
  ...

    def varietal_list(self):
        return ', '.join([v.name for v in self.varietal.all()])

    def set_default_varietals(self):
        if self.varietal.count() == 0:
            # add all default varietals
            for v in Varietal.default():
                self.varietal.add(v)
            print self.varietal_list()  # check to see that it
happened

    def save(self):
        super(Vineyard, self).save() # make sure we have an ID before
we do m2m
        self.set_default_varietals()


views.py:

def test(request):
    # this one works just fine
    v = Vineyard.objects.get(id=368)
    v.save()
    return HttpResponse(v.varietal_list())

==============================

When vineyard.save() is called on a new or existing object from a view
or a standalone script, it works just fine and the m2m table in the
database is updated correctly.

When I do a Save from the Admin page, I can see from the print
statement (running dev server) that set_default_varietal() is called
and that (apparently) the association was made, BUT(!) in the next
view of this Vineyard object the association is missing! (!!!)

There's no error message or any other indication that something wasn't
working, except that the new m2m relationships aren't there in the
database.

Is there something I'm missing here?

I tried adding another super().save() after set_default_vineyards()
and that didn't make any difference. I tried calling
transaction.commit_unless_managed(), no change. I tried calling
transaction.commit() and got a Transactional Error.

Help!

Ps. Although this one might be solvable by setting the defaults when a
new record is created (but where do you do that for an m2m?), there is
a *much* more complicated variation of this that keys off an analysis
of the object being save()'ed. That one must use something like this
code.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to