Django - foreignkey problem
Posted
by realshadow
on Stack Overflow
See other posts from Stack Overflow
or by realshadow
Published on 2010-05-04T15:25:52Z
Indexed on
2010/05/04
15:28 UTC
Read the original article
Hit count: 260
Hey,
Imagine you have this model:
class Category(models.Model):
node_id = models.IntegerField(primary_key = True) type_id = models.IntegerField(max_length = 20) parent_id = models.IntegerField(max_length = 20) sort_order = models.IntegerField(max_length = 20) name = models.CharField(max_length = 45) lft = models.IntegerField(max_length = 20) rgt = models.IntegerField(max_length = 20) depth = models.IntegerField(max_length = 20) added_on = models.DateTimeField(auto_now = True) updated_on = models.DateTimeField(auto_now = True) status = models.IntegerField(max_length = 20) node = models.ForeignKey(Category_info, verbose_name = 'Category_info', to_field = 'node_id'
The important part is the foreignkey. When I try:
Category.objects.filter(type_id = type_g.type_id, parent_id = offset, status = 1)
I get an error that get returned more than category, which is fine, because it is supposed to return more than one. But I want to filter the results trough another field, which would be type id (from the second Model)
Here it is:
class Category_info(models.Model):
objtree_label_id = models.AutoField(primary_key = True) node_id = models.IntegerField(unique = True) language_id = models.IntegerField() label = models.CharField(max_length = 255) type_id = models.IntegerField()
The type_id can be any number from 1 - 5. I am desparately trying to get only one result where the type_id would be number 1.
Here is what I want in sql:
SELECT n.*, l.*
FROM objtree_nodes n
JOIN objtree_labels l ON (n.node_id = l.node_id)
WHERE n.type_id = 15 AND n.parent_id = 50 AND l.type_id = 1
Any help is GREATLY appreciated.
Regards
© Stack Overflow or respective owner