On Feb 5, 3:59 pm, knight <alexar...@gmail.com> wrote: > Hi, > > I have the following classes in my models.py: > > class Default(models.Model): > name = models.CharField(max_length=50, choices = > (('rental_period', 'rental_period'), ('currency', 'currency')), > unique=True) > value = models.CharField(max_length=50) > > class Media_biz(models.Model): > name = models.CharField(max_length=50) > currency = models.ForeignKey(Currency) > rental_period = models.CharField(max_length=5, blank=True, > null=True) > > What I want is: > When I add new Media_biz in my admin page, I want to check if there is > a Default object with the name "currency" or "rental_period" and take > it's value as default. > > I tried to use "default" field option for this, but I can't insert > logic that checks Default objects in models.py, because it fails on > syncdb if my db is empty. > > Does anybody have a solution how can I do that? > > Thanks, > Arshavski Alexander.
You can override the save method in the Media_biz model to do this check before saving. def save(self, force_insert=False, force_update=False): if not self.rental_period: try: default = Default.objects.get(name='rental_period') except Default.DoesNotExist: pass else: self.rental_period = default.value # and something for currency - but see below super(Media_biz, self).save(force_insert, force_update) There's a piece of information missing before you can do the same with currency, though. Currency is a foreign key, so what are you storing in the Default model against 'currency'? Is it the value of a field on the currency table? -- 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 -~----------~----~----~----~------~----~------~--~---