wondering how to do something similar to save_model for inline.

1,624 views
Skip to first unread message

Timboy

unread,
Dec 27, 2008, 7:46:03 PM12/27/08
to Django users
I have an admin object where I want to pass in the request.user on
save. It works fine in my normal admin class but not for the inline
version.

(works fine here):
class RentAdmin(admin.ModelAdmin):
exclude = ['rented_by']
list_display = ('renter', 'movie', 'late', 'owed', 'paid',
'due_date', 'rented_by')
sort_by = '-due_date'
raw_id_fields = ('renter', 'movie')

(but not here):
class rentalInline(admin.TabularInline):
model= Rent
extra = 3
raw_id_fields = ('movie',)
exclude = ['rented_by']

def save_model(self, request, obj, form, change):
if not change:
obj.rented_by = request.user
obj.save()

Any help would be appreciated.

alex....@gmail.com

unread,
Dec 27, 2008, 9:35:20 PM12/27/08
to Django users

Timboy

unread,
Dec 27, 2008, 10:32:25 PM12/27/08
to Django users
Great link. I was really excited to try it.

Here is my new inline:
class rentalInline(admin.TabularInline):
model= Rent
extra = 3
raw_id_fields = ('movie',)
exclude = ['rented_by']

def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
instance.rented_by = request.user
instance.save()
formset.save()

I am still getting the same issue:
Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in root
157. return self.model_page(request, *url.split('/',
2))
File "/usr/lib/python2.5/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in model_page
176. return admin_obj(request, rest_of_url)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in __call__
197. return self.change_view(request, unquote(url))
File "/usr/lib/python2.5/site-packages/django/db/transaction.py" in
_commit_on_success
238. res = func(*args, **kw)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in change_view
583. self.save_formset(request, form, formset,
change=True)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in save_formset
382. formset.save()
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in save
372. return self.save_existing_objects(commit) +
self.save_new_objects(commit)
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in
save_new_objects
407. self.new_objects.append(self.save_new(form,
commit=commit))
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in
save_new
473. return save_instance(form, new_obj, exclude=
[self._pk_field.name], commit=commit)
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in
save_instance
59. instance.save()
File "/home/richard/work/svn/moviedb/../moviedb/store/models.py" in
save
91. super(Rent, self).save(**kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in
save
307. self.save_base(force_insert=force_insert,
force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in
save_base
379. result = manager._insert(values,
return_id=update_pk)
File "/usr/lib/python2.5/site-packages/django/db/models/manager.py" in
_insert
138. return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in
insert_query
888. return query.execute_sql(return_id)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/
subqueries.py" in execute_sql
308. cursor = super(InsertQuery, self).execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py"
in execute_sql
1700. cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in
execute
19. return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /admin/store/renter/4/
Exception Value: null value in column "rented_by_id" violates not-null
constraint

Praveen

unread,
Dec 29, 2008, 12:10:51 AM12/29/08
to Django users
This is your database error..

Timboy

unread,
Dec 29, 2008, 5:33:43 PM12/29/08
to Django users
DB is sqlite3. Deleted DB file syncd and still same traceback.
here is proper traceback paste: http://dpaste.com/103517/

Timboy

unread,
Jan 6, 2009, 10:10:33 PM1/6/09
to Django users
Still no solution to this. It has been confirmed by another user. Is
it a bug?

Karen Tracey

unread,
Jan 7, 2009, 12:23:17 AM1/7/09
to django...@googlegroups.com
On Tue, Jan 6, 2009 at 10:10 PM, Timboy <corn1...@gmail.com> wrote:

Still no solution to this. It has been confirmed by another user. Is
it a bug?

(Where was it confirmed by another user?  I don't see that.)

The doc list "save_formset" under "ModelAdmin methods" yet you seem to have added it to your inline admin class.  I also don't see any indication in the traceback you posted that your custom method is being called, which leads me to think where you have put it is not where it needs to be in order for it to be called.

(Also you changed formset.save_m2m() from the example in the docs to formset.save().  I doubt that this change is correct.)

Karen

Timboy

unread,
Jan 7, 2009, 3:03:54 AM1/7/09
to Django users
Good call on the ModelAdmin spot. (user contacted me by email to see
if I had a solution yet.)

I got the formset.save() from here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
saying: "If your formset contains a ManyToManyField, you'll also need
to call formset.save_m2m() to ensure the many-to-many relationships
are saved properly."

Works now just had it in the wrong spot. thx!

On Jan 6, 9:23 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages