Reputation: 13308
I've got a simple django model with 2 optional fields. I'm getting an error when I try to save the model.
class Cart(models.Model):
shipping = models.ForeignKey(ShippingRate, blank=True, null=True)
date_created = models.DateTimeField(blank=True, null=True)
def save(self, *args, **kwargs):
if not self.date_created:
self.date_created = datetime.datetime.now()
super(Cart, self).save(*args, **kwargs)
Using a simple view to save a cart object, I get the following error -
'Cart' object has no attribute 'shipping_id'
It's the call to super()
that's failing. I've had a look at the database and I've got the fields 'id'
, 'date_created'
and 'shipping_id'
on my table.
I'm managing my db with south, and I've tried removing, migrating and replacing fields but that doesn't work. It's a strange one - I expect I'm missing something obvious but I haven't spotted it yet..
UPDATE
I've tried removing the save()
override, and even swapping out fields (then migrating with south). I swapped out the shipping
field for a new shipping_rate
field -
class Cart(models.Model):
shipping_rate = models.ForeignKey(ShippingRate, blank=True, null=True)
date_created = models.DateTimeField(blank=True, null=True)
This time I get the same error on save, only it's the date_created field that's causing it.
'Cart' object has no attribute 'date_created'
As before, I've had a glance at my (postgres) database, and I can see the 3 fields I expect - id
, shipping_rate_id
, and date_created
.
UPDATE 2
OK, I've reduced my code to -
#models
class Cart(models.Model):
shipping_rate = models.ForeignKey(ShippingRate, blank=True, null=True)
modified = models.DateTimeField(blank=True, null=True)
class Meta():
app_label = 'cart'
#views
def add_to_cart(request):
if 'cart' in request.session:
cart = request.session['cart']
else:
cart = Cart()
if not cart.pk:
cart.save()
return HttpResponse('ok')
And my traceback -
Traceback:
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/aidan/Code/bbhq/bbhq/cart/views.py" in add_to_cart
71. cart.save()
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/db/models/base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1575. query.insert_values(fields, objs, raw=raw)
File "/Users/aidan/Environments/bbhq/lib/python2.7/site-packages/django/db/models/sql/subqueries.py" in insert_values
170. value = getattr(obj, field.attname)
Exception Type: AttributeError at /cart/add_to_cart
Exception Value: 'Cart' object has no attribute 'shipping_rate_id'
Upvotes: 0
Views: 776
Reputation: 599490
As mentioned in the comment, there was an old entity in the session that was pickled before the new fields were added.
Upvotes: 2