On Sun, 2006-07-16 at 15:46 -0700, webd0012 wrote:
> Thanks for your reply.  I'm trying what you said but I can't get it to
> work.  If I put class "City" first, "Place" doesn't work, and if I put
> another class first, other classes get errors.  Not sure what is wrong
> with the order.
> 
> from django.db import models
> 
> class City(models.Model):
>     CITIES = (
>         ('Boston', 'Boston'),
>         ('Worcester', 'Worcester'),
>         ('Springfield', 'Springfield'),
>     )
>     city_name = models.CharField(maxlength=40, choices=CITIES)
>     place = models.OneToOneField(Place)
> 
>     def __str__(self):
>         return self.city_name
> 
>     class Admin:
>         pass
> 
> class Place(models.Model):
>     place_name = models.CharField(maxlength=200)
>     CitySection = models.ForeignKey(CitySection)

Since you have not yet defined the CitySection class (it comes later in
the file), you cannot refer to it by reference here. Instead, to make
forward references like this, use the name of the class as a string:

        CitySection = models.ForeignKey('CitySection')
        
You did not mention which errors you were seeing, so I have no idea if
this is the only problem, but it was something that jumped out at me as
I read through your fragment.
        
You might also have problems with an attribute and a class name being
exactly the same here. They are mostly in different name spaces, but at
construction time things go a little weird, so I'm not 100% certain
about this. "Typical" Python naming style is to not use initial caps on
attributes, even if you are adopting a Camel Casing strategy throughout
(although you seem to be mostly using underscore separators, so you
might as well just go all out for consistency here).

> 
>     def __str__(self):
>         return self.place_name
> 
>     class Admin:
>         pass
> 
> class Park(models.Model):
>     has_benches = models.BooleanField() # just a test
>     place = models.OneToOneField(Place)
> 
>     class Admin:
>         pass
> 
> class CitySection(models.Model):
>     SECTIONS = (
>         ('West Boston', 'West Boston'),
>         ('South Boston', 'South Boston'),
>         ('West Springfield', 'West Springfield'),
>         ('Central Worcester', 'Central Worcester'),
>         ('South Worcester', 'South Worcester'),
>     )
>     city_section = models.CharField(maxlength=5, choices=SECTIONS)
>     city = models.ForeignKey(City)
> 
>     def __str__(self):
>         return self.city_section
> 
>     class Admin:
>         pass

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to