Hi, On Fri, 2008-05-09 at 10:47 -0700, Chris Farley wrote: > I'm trying to model our customers in a Django application, but I'm > encountering a problem: > > Customers can have many CustAddresses > One CustAddress is the Customer's primary billing address > One CustAddress is the Customer's primary shipping address > > It's a classic 'chicken and egg' problem. Customers have a CustAddress > foreign key, and CustAddress has a Customers foreign key. When running > syncdb, I get this error: > > NameError: name 'Customer' is not defined > > What's the way around this? > > Here's my models.py: > > class CustAddress(models.Model): > customer = models.ForeignKey(Customer) > name = models.CharField(max_length=50) > address1 = models.CharField(max_length=50) > address2 = models.CharField(max_length=50, blank=True) > city = models.CharField(max_length=50) > region = models.CharField(max_length=50) > postal_code = models.CharField(max_length=50) > country_code = models.CharField(max_length=2) > residential = models.BooleanField(default=True) > > class Customer(models.Model): > number = models.CharField(max_length=15, unique=True) > first_name = models.CharField(max_length=35) > last_name = models.CharField(max_length=35) > phone_no1 = models.CharField(max_length=20, blank=True) > phone_no2 = models.CharField(max_length=20, blank=True) > user = models.ForeignKey(User, unique=True) > created_at = models.DateTimeField(editable=False) > updated_at = models.DateTimeField(editable=False) > primary_bill_address = models.ForeignKey(CustAddress) > primary_ship_address = models.ForeignKey(CustAddress)
The solution for this problem is well documented here: http://www.djangoproject.com/documentation/model-api/#many-to-one-relationships -- http://spinlock.ch/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---