On 10/1/07, Paddy Joy <[EMAIL PROTECTED]> wrote: > > I have a many to one relationship between domain and DNSRecord. When > adding a domain I need to add some default DNSRecords. I do this after > saving the domain using the following code. > > > domain = form.save(commit=True) > > d = DNSRecord(domain=domain, type='NS', data='ns1.mydomain.com', > aux=0, ttl=7200) > new_d = d.save() > > d = DNSRecord(domain=domain, type='NS', data='ns2.mydomain.com', > aux=0, ttl=7200) > new_d = d.save() > > d = DNSRecord(domain=domain, type='NS', data='ns3.mydomain.com', > aux=0, ttl=7200) > new_d = d.save() > > d = DNSRecord(domain=domain, type='NS', data='ns4.mydomain.com', > aux=0, ttl=7200) > new_d = d.save() > > > It works but it's not very nice because the default records are hard > coded into the view. Does anyone have any suggestions for a better > approach? > > Paddy
I would override the Domain model's save method for that. Something like: class Domain(models.Model): # ... def save(self, **kwargs): is_new = not self.id super(Domain, self).save(**kwargs) if is_new: for i in range(1, 5): self.dns_records.create(data='ns%s.mydomain.com' % i, type='NS',aux=0, ttl=7200) class DNSRecord(models.Model): # ... domain = models.ForeignKey(Domain, related_name='dns_records') # ... Jonathan. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---