Dnia 16-08-2010 o 19:25:50 Alec Shaner <asha...@chumpland.org> napisaĆ(a):
Regarding your issue with get_next, could be because you're invoking the
method when you define default=get_next(). Try it with just the bare
method
name get_next.
You could also use the aggregate function instead of creating a new
model:
Thanks! I figured that out:
from django.db.models import Max
def get_val(dictionary):
try:
return dictionary['number__max'] + 1
except:
return 0
class Newspaper(models.Model):
number = models.PositiveIntegerField(unique = True, default = lambda
: get_val(Newspaper.objects.aggregate(Max('number'))))
http://docs.djangoproject.com/en/1.2/topics/db/aggregation/#topics-db-aggregation
Just define get_next to call the Max aggregate on your Newspaper model's
number field.
On Mon, Aug 16, 2010 at 12:39 PM, bagheera <neost...@go2.pl> wrote:
Hi, i have "number" field in "newspaper" model. I want assign to it
dynamically a default value, witch would be a incremented by 1 highest
value
so far.
So i managed to make a little workaround, since i can't query for last
value of "number" field of model from model itself.
I have created new model, witch has only 1 field any 1 record, witch
stores
current highest value.
I wrote two functions:
#-*- coding: utf-8 -*-
from nml.options.models import Wydanie_opt
def set_next(value):
try:
option = Wydanie_opt.objects.all()[0]
if value > option.nr_wydania_next:
option.nr_wydania_next = value
option.save()
except:
option = Wydanie_opt(nr_wydania_next = value)
option.save()
def get_next():
try:
option = Wydanie_opt.objects.all()[0]
value = option.nr_wydania_next
except:
return 0
else:
return value + 1
Here's code of "newspaper" model:
from nml.options.utils import get_next
class Wydanie(models.Model):
nr_wydania = models.PositiveIntegerField(verbose_name = "Numer
wydania",
unique = True, default = get_next(),
help_text = "Unikalny numer wydania.")
The problem is, set_next is working as intended, but get_next() does
NOT,
default value stays the same until i restart dev server. Why? Query
isn't
evaluated? get_next() function isn't called at all?
Mb there is a better way to implement auto-incrementation.
Keep in mind that, "nr_wydania" field must be visible and editable,
since
first entered value is unknown, some values may be skipped, and there
is no
order of adding it.
--
Linux user
--
You received this message because you are subscribed to the Google
Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
Linux user
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@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.