stava wrote:
> I'm having a bit of a problem with dynamic choices. I'm working on a
> simple timesheet system where I've extended the auth.users with a
> couple of fields including something I call "primary group":
>
> def getUsers(group = None):
>   users = []
>   if group is None:
>     for u in auth.users.get_values(fields = ('id', 'username',)):
>       users.append((u['id'], u['username']))
>   else:
>     for u in auth.users.get_values(
>           fields = ('id', 'username',),
>           tables = ['auth_users_groups', 'auth_groups'],
>           where  = ["auth_groups.name = '%s'" % group,
>                     'auth_users_groups.group_id = auth_groups.id',
>                     'auth_users_groups.user_id = auth_users.id']):
>       users.append((u['id'], u['username']))
>   return users
>
> class Department(meta.Model):
>   department = meta.CharField(maxlength = 10, unique = True,
>                  help_text = 'Short name')
>   name       = meta.CharField(maxlength = 30, unique = True,
>                  help_text = 'Full name')
>   costcentre = meta.CharField(maxlength = 10)
>   manager    = meta.ForeignKey(auth.User, verbose_name = 'manager',
>                  choices = getUsers('Line Manager'),
>                  help_text = 'Department manager')
>
> class User(auth.User):
>   primary_group = meta.ForeignKey(auth.Group, default = 1,
>                     verbose_name = 'primary group')
>   department    = meta.ForeignKey(Department, default = 1)
>   country       = meta.ForeignKey(Country, default = 1,
>                     help_text = 'Country the user belongs to')
>   notes         = meta.TextField(blank = True)
>
> Now comes the problem, and I can't decide if it's a bug or if I am not
> using 'choices' the right way: when initialising a freshly created
> database, I get:
>
> django-admin init tttime
> Error: The database couldn't be initialized.
> (1146, "Table 'tttime.auth_users' doesn't exist")
>
> However, if I comment out the 'choices' part of the 'manager'
> definition in the Department class, it works like a charm. Then I can
> uncomment the 'choices' and everything works as expected.
>
> Please advice anyone? Is there a better way to implement dynamic
> choices?
>
> /stava

And, of course, if one were to read the documentation properly:| the
solution is to use 'limit_choices_to'.

/stava

Reply via email to