views:

17

answers:

1

Hi guys, i having problem with this:

model.py (1)

class Profession(models.Model):
    user= models.ForeignKey(User,unique=True)
    principal_area = models.ForeignKey(Area,verbose_name='Area principal',related_name='area_principal')
    others_areas = models.ManyToManyField(Area)

model.py (2)

class Area(models.Model):
    area = models.CharField(max_length=150,unique=True)
    slug = models.SlugField(max_length=200)    
    activa = models.BooleanField(default=True)

In Model 1 i have a field "principal_area" and other "others_areas".

How ill list all professional where "principal_area" OR "others_areas" are in Area model from my views?

Sorry if im not too clear

+2  A: 

Take a look at Django's Q objects. Here is an example of how you could go about this:

area = Area.objects.get(**conditions)
Profession.objects.filter(
    Q(principal_area = area) | Q(others_areas__in = [area])
)
Manoj Govindan
+1, Beat me to it, by a minute. :)
Lakshman Prasad
@Lakshman: prepped up on too much green tea after dinner :P
Manoj Govindan
A great answer, though I'd also suggest to use OneToOneField instead of ForeignKey field for principal_area.
Ihor Kaharlichenko