On Tue, 2008-02-12 at 13:36 +0100, Hanne Moa wrote:
> On Feb 12, 2008 12:51 PM, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> > On Tue, 2008-02-12 at 12:44 +0100, Hanne Moa wrote:
> > > On Feb 12, 2008 12:16 PM, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> > > > On Tue, 2008-02-12 at 12:11 +0100, Hanne Moa wrote:
> > > > > Each org only has a single region, and in the old web-interface the
> > > > > region-data is shown as a drop-down-list (<select>) where you can
> > > > > choose one and only one. So: org.region == region.id, there can be
> > > > > many orgs in a region but only one region per org.
> > > >
> > > > Which means it's many-to-one, not one-to-one. If you were to draw a
> > > > picture of your table rows, many rows would (potentially) be pointing to
> > > > a single row in the org table. That's many-to-one.
> > > >
> > > > In Django a many-to-one relation is represented by a ForeignKey field.
> > >
> > > I'm using a ForeignKey-field for it now, but I don't see anywhere how
> > > to make it behave as in the old system: in Django's admin-interface
> > > for org the foreignkey is shown as a drop-down *but with nothing
> > > selected*.
> > >
> > > When looking at the examples in the tutorial: Poll/Choice, a Poll can
> > > have several Choices, but a Choice can have only a single Poll. But
> > > org->region is *the other way around*. One Poll containing many
> > > Choices vs. one org having one region. The fact that one region have
> > > many orgs is irrelevant as region will never be editable or visible
> > > anywhere else but as linked from org.
> > >
> > > So, how do I do that?
> >
> > Put the ForeignKey on the "many" side of the relation. In your case, it
> > looks like it goes on the "org" model, since there are many orgs to a
> > region.
> 
> I already have:
> 
> class Region(models.Model):
>     id = models.IntegerField(unique=True, primary_key=True)
>     region = models.CharField(maxlength=32)
>     location = models.CharField(maxlength=32)
>     class Meta: db_table = 'region'
> 
>     def __str__(self):
>         return '%s (%s)' % (self.region, self.lokasjon)

Here's the problem: your __str__ method will be throwing an exception
because the lokasion attribute doesn't exist.

> 
> class Org(models.Model):
>     id = models.IntegerField(unique=True, primary_key=True)
>     ..
>     region = models.ForeignKey(Region, db_column = 'id') #x, Region.id

By default, ForeignKeys refer to the primary key of the related model.
So you can do away with the db_column attribute here if you want to be
more succient.

[...]
> http://www.djangoproject.com/documentation/faq/
> 
> This FAQ, which I read before I came here, and which I assume is
> pretty official, doesn't contain answers to my type of question. So,
> is there another FAQ for such? The obvious google didn't find any.

Any FAQ file is a subset of frequently ask questions. It doesn't contain
every single question ever asked. Not even every question asked more
than once. However a lot of questions of already been asked previously
on this very mailing list. So I am suggesting that you do the natural
things and search the mailing list archives (or just use Google and
don't even restrict it to the django-user archives). View the mailing
list as an ever-growing, live list of frequently asked questions (and
multiple answers).

Regards,
Malcolm

-- 
Borrow from a pessimist - they don't expect it back. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to