views:

57

answers:

2

I'm completely stumped as to why this isn't working:

flight = Flight.objects.get(pk=flight_id)
print "old", flight.route.pk ## `route` is a ForeignKey field to model Route
print "new", new_route.pk
flight.route=new_route  # new_route is a newly created Route object
flight.save()
print "db", Flight.objects.get(pk=flight_id).route.pk

this is the output:

old 4800
new 7617
db 4800

Is there some special way I need to call save() on the flight to get it to actually save?

edit: my models look like this:

class Flight(models.Model):
    route = models.ForeignKey(Route, blank=True, null=True, related_name="flight")

class Route(models.model):
    # a bunch of CharFields and IntegerFields
+1  A: 

Has the new_route been saved? Assuming pk would not return a result, but unable to test.

Otherwise see http://code.djangoproject.com/ticket/8892

SeriousCallersOnly
pk will return None if the model instance has not been saved.
Andre Miller
A: 

OK I just figured it out, I recently moved my custom save function from the bottom of the class definition to the top, and I forgot the last line that calls super(Flight, self).save(*args, **kwargs)

nbv4