Hi,
>
> from django.dispatch import dispatcher
> from django.db.models import signals
>
> from hgfront.issue.models import *
>
> def init_issues_database():
> """This injects the database with default values for issues"""
> issue_types = (
> ('Bug', 0),
> ('Enhancment', 1),
> ('Feature Request', 2),
> ('Typo', 3)
> )
> for issue in issue_types:
> p = IssueType(title = issue[0], order=issue[1])
> p.save()
Replace those 2 statements with this:
IssueType.objects.get_or_create(title=issue[0],
defaults={'order':issue[1]})
This will ensure that an IssueType gets created only if that title
doesn't already exist.
> for sev in sev_types:
> s = IssueSeverity(title = sev[0], order=sev[1])
> s.save()
Repeat the above pattern for IssueSeverity here.
> for status in status_types:
> t = IssueStatus(title = status[0], order=status[1])
> t.save()
Repeat here too.
> The problem is, when I do syncdb, the items do get added to the
> database, but for issue_types instead of just 4 entries, 36 entries go
> in. For sev_types and status_types 40 entries go in each instead of
> 5. From what I can work out, it seems to be looping and adding a full
> entry for each item within the tuples.
That's because your management.py can get invoked more than once. So,
you do want to defend your code against that with the "get_or_create"
method.
-Rajesh
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---