Hello, Thanks for your reply Daniel, that along with setting ordering on the Comic model seems to have fixed it.
Titus On Dec 28, 9:55 am, Daniel Roseman <roseman.dan...@googlemail.com> wrote: > On Dec 28, 5:35 am, tsop <tso...@gmail.com> wrote: > > > > > Hey, > > I'm quite new to django but I ran into a small problem maybe someone > > can shed some light onto: > > > models.py > > > class Comic(models.Model): > > title = models.CharField(max_length=80, help_text='Title of > > comic.') > > series = models.ForeignKey(ComicSeries, help_text='Which > > series comic belongs to.') > > image = models.ImageField(upload_to='tmp', help_text='A > > comic.') > > page_number = models.IntegerField(editable=False) > > def __unicode__(self): > > return "%s - %s" % (self.title, self.series) > > > def get_absolute_url(self): > > #return "/comics/%s/?page=%d" % (self.series.slug, > > self.page_number) > > return "/comics/%s/%d" % (self.series.slug, > > self.page_number) > > > def save(self): > > if not self.page_number: > > count = len(Comic.objects.filter > > (series=self.series)) > > self.page_number = count + 1 > > > super(Comic, self).save() > > > -------------------------------------- > > > admin.py > > > class ComicInline(admin.StackedInline): > > model = Comic > > extra = 20 > > > (Multi form uploading) > > > When saving a comic it should also save the right page number, the > > reason I'm using my own page_numbers is because each comic belongs to > > a series and if its the first in that series it should be page number > > one, so we're not using comic id or anything. > > > It works for the most part, the page number increments as the comics > > are saved, but when you start deleting previous comics and adding new > > ones it doesn't save in the right order anymore. > > > All I really need to know is if there's a way we can make sure that > > when multi form saves, it saves them in either the right order, or pre- > > populates the page_number field with the right values. > > Instead of getting the total number of comics in the series, what you > want to do is get the highest existing page number. This is one way to > do it: > Comic.objects.filter(series=self.series).order_by('-page_number') > [0].page_number > > This just reverse-orders the comic series by page number, and takes > the page_number from the first object - which will be the highest. > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---