Re: Inheritance through a ForeignKey field

2006-12-01 Thread phess

I just saw the source of all my problems.

This is the code for the Article class:

class Article(models.Model):
   """This class defines an Article, with links to:
   - a Magazine,
   - a Section (which depends on the Magazine), and
   - an Issue (which also depends on the Magazine)"""

   def __str__(self):
  return self.nicetitle
   class Admin:
  pass

   title = models.CharField(maxlength=40, core=True)
   nicetitle = models.SlugField(prepopulate_from=('title'))
   magazine = models.ForeignKey(Magazine, edit_inline=False)

   # Hard part
   validsections = # How do I refer to the value (i.e., name or id) of
the magazine variable created right above?

Cheers


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



Re: Inheritance through a ForeignKey field

2006-12-01 Thread phess

Sorry, I forgot to actually *ask* the question. :)

I'm able to get a list of Sections related to 'Linux Magazine', for
example, with:

validsections = magazine.section_set.all()

This returns this list:
[, ]  # (so far I've created only
these 2 sections)

Is there a method to restrict the choices in the following line, like
this?:

section = models.ForeignKey(Section, choices=validsections)

I tried this, but got this error:
'ForeignKey' object has no attribute 'section_set'

Once again, thanks for any help.


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



Re: Inheritance through a ForeignKey field

2006-12-01 Thread phess

It did help. Thank you very much.
But the situation has changed already. :\

I'd like the Admin pages to show me only the Sections and Issues that
make sense for the Magazine I choose from the selectbox.

So, this is how my model is (sort of):

I have a Magazine class, an Issue class and a Section class.

A Magazine has many issues.
A Magazine has many sections.
Classes Section and Issue are *not* directly connected.

So,

class Section(...):
  ...
  magazine = models.ForeignKey(Magazine)


class Issue(...):
  ...
  magazine = models.ForeignKey(Magazine)

That works perfectly so far.

However, I want to add an Article, and after I choose a magazine, I
want to restrict the choices of Sections and Issues to the ones linked
to that specific magazine.

The result should be:
> User chooses 'Linux Magazine'
>> User gets to choose from all the Sections that belong to the 'Linux 
>> Magazine' instance, and *no Section* that belongs to 'Easy Linux'.
>> User gets to choose from all the Issues that belong to 'Linux Magazine', and 
>> only those.

PS: If it matters to anyone, this is for the brazilian branch of Linux
New Media AG, the publishers of Linux Magazine (international) and
Linux Magazin (Germany), and also Linux Magazine (Brazil) and Easy
Linux (Brazil).


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



Re: Inheritance through a ForeignKey field

2006-11-30 Thread yun

Suppose that you have a Section model that has a foreign key to
Magazine, and the field in the Section model is called magazine. You
can then do something like this once you get the section:

s = Section.objects.select_related.get()
s.magazine.id will return the id of the magazine, if it has a name then
s.magazine.name will return the name.

This will do the all the related querries in one shot. You can also do:

s = Section.objects.get()
s.magazine.id will still return the mangazine id, but it will also
query the database twice. Once to get the Section object, then second
time to get the details fo the magazine object.

Assuming that you want to put this into a form, you'll probably have to
do something like:

new_data['magazine'] = s.magazine.id

in the forms page.

Hope that this helps.


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



Inheritance through a ForeignKey field

2006-11-30 Thread phess

I'm now building an online magazine website. Each magazine has the same
set of sections. And every article belongs to one and only one section.

So, the Article class has a ForeignKey(Section) line.

So far, so good (I guess). :)

But there are actually 2 magazines, each with its own group of
sections. When I create an article, I want the "Section" field in the
form to automatically load the "Magazine" field in the same form.

Does django handle this kind of thing?


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