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.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.