On 6/15/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> All,
>   I have been trying to create a model for a project that I'm starting
> with the trunk version of Django(revision 3129 or newer).  In the
> following example I'm trying to create a model that allows the site
> administrator(s) to create locations based on Country, State, and City
> then associate them with a Job.  In the Admin I can create the Country,
> State, and City records/objects with the appropriate relationships.
> However when I relate them with the Job class the Admin doesn't
> restrict the selections with there respective constraints.  For
> example, I can create a new job object with the following information:
> Country = US, State = Kansas, City = Chicago despite the fact that I
> created that object/record with the following relationships: Country =
> US, State = Kansas, City = Lawrence.  How can I limit the choices in
> the Admin interface for Country, State, and City with there appropriate
> relationships when I create Job objects?  I have spent quite a bit of
> time looking/Googleing for a clear example of how to do this, but
> haven't had any luck.  Could you please look over this example model
> and modify it so that it works as desired, and post it back to the
> list.  It would probably be a good addition to the documentation as
> well, so I will submit it as a documentation example, or add it to the
> cookbook once things are hashed out.
>
> Thanks For Your Help!
> -Nick
>
> Example Model:
> ######################################################################################
>
> from django.db import models
>
> class Country(models.Model):
>     name = models.CharField(maxlength=100, core=True, unique=True)
>
>     def __str__(self):
>         return (self.name)
>
>     class Admin:
>         pass
>
> class State(models.Model):
>     country = models.ForeignKey(Country)
>     name = models.CharField(maxlength=100, unique=True, core=True)
>
>     def __str__(self):
>         return (self.name)
>
>     class Admin:
>         pass
>
> class City(models.Model):
>     region = models.ForeignKey(Region)
>     name = models.CharField(maxlength=100, unique=True, core=True)
>
>     def __str__(self):
>         return (self.name)
>
>     class Admin:
>         pass
>
> class Job(models.Model):
>     created_on = models.DateTimeField(auto_now_add=True)
>     country = models.ForeignKey(Country)
>     region = models.ForeignKey(State)
>     area = models.ForeignKey(City)
>     title = models.CharField(maxlength=100)
>     summary = models.CharField(maxlength=500)
>     body = models.TextField(blank=True, help_text="Optional")
>     image = models.URLField(verify_exists=True, blank=True,
> help_text="Optional")
>     file = models.URLField(verify_exists=True, blank=True,
> help_text="Optional" )
>
>
>     def __str__(self):
>         return (self.title)
>
>     class Admin:
>         pass
>
>
> >
>

Change your model to only link from Job to City. Then the State and
Country values can be inferred from traversing through your model.

Because you've got independent links to Country, State and City they
don't need to be dependent.

Oh, and you should probably standardise on using either Region or
State, at the moment you appear to be using the terms interchangeably
and it makes your model slightly confusing.

Regards,
Andy
-- 
>From the desk of Andrew J Todd esq

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

Reply via email to