Code

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#302 closed defect (fixed)

object.save() doesn't update, re-inserts

Reported by: Manuzhai Owned by: adrian
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: paul.bowsher@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Probably something to do with [462]. My model:

class Post(meta.Model):
    fields = (
        meta.MZSlugField('slug', 'slug', prepopulate_from = ('title',)),
        meta.CharField('title', 'title', maxlength = 255),
        meta.DateTimeField('datetime', 'date created'),
        meta.DateTimeField('modified', 'date modified'),
        meta.TextField('content', 'post'),
        meta.ManyToManyField(Tag, filter_interface = meta.HORIZONTAL),
    )
    admin = meta.Admin(
        fields = (
            (None, {'fields': ('title', 'slug', 'datetime', 'content', 'tags')}$
        ),
        list_display = ('title', 'datetime', 'display_tags'),
        list_filter = ('datetime', 'tags'),
    )
    ordering = ('-datetime',)
    def __repr__(self):
        return self.title
    def display_tags(self):
        return ' '.join([obj.tag for obj in self.get_tag_list()])
    display_tags.short_description = 'Tags'

I'm doing this with the API, from the interactive interpreter.

Attachments (0)

Change History (9)

comment:1 Changed 9 years ago by adrian

Could you paste in the API code you used to save? Our unit tests are passing, so I suspect your API calls might be strange.

comment:2 Changed 9 years ago by Manuzhai

Was really basic, something like this:

>>> from django.models.posts import posts, tags
>>> mv = tags.get_object(tag__exact = 'movies')
>>> post = posts.get_object(slug__exact = 'you-stupid-man')
>>> post.set_tags([mv.id])
>>> post.save()
>>> post.save()

This is with [473].

comment:3 Changed 9 years ago by jacob

Why are you calling save() twice?

comment:4 Changed 9 years ago by Manuzhai

That was to test that it was inserting another time. That shouldn't re-insert the object, though, should it?

comment:5 Changed 9 years ago by adrian

You don't need to call post.save() after calling post.set_tags().

You only need post.save() to save fields on the object itself. It has no effect on related objects.

comment:6 Changed 9 years ago by Manuzhai

Okay, that actually makes sense. But still, why is it re-inserting?

comment:7 Changed 9 years ago by Boffbowsh

  • Cc paul.bowsher@… added

I'm getting a similar problem with the admin interface. If nothing has changed and then you hit save, it tries to reinsert, but fails as my primary key isn't an autofield

class Player (meta.Model):
	fields = (
		meta.IntegerField('profileid', primary_key=True),
		meta.CharField('realname', maxlength=32, blank=True),
		meta.IntegerField('bookingid'),
		meta.CharField('ingamename', maxlength=32, db_index=True, unique=True),
		meta.ForeignKey(Team, blank=True),
		meta.ForeignKey(Server, name="lastseenon")
	)

	def __repr__(self):
		return self.ingamename
	
	admin = meta.Admin(
			search_fields = ['ingamename', 'profileid'],
		)

comment:8 Changed 9 years ago by Manuzhai

I think this is probably fixed as a result of [507], see also #320.

comment:9 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

Good call, Manuzhai. This should be fixed now.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.