Eastwood
Eastwood

Reputation: 77

Accessing values from many-to-many fields in django

I have a model with many-to-many field. this field points to pk of another table. i want to pull the records which will be filtered upon 'type' field of Datastore model. following are the models.

class VM(models.Model):
   name = models.CharField(max_length=100,null=True,blank=True)
   power_state = models.CharField(max_length=100,null=True,blank=True)
   memory_size = models.BigIntegerField(null=True,blank=True)
   d_store = models.ManyToManyField(Datastore, null=True,blank=True)


class Datastore(models.Model):
   datacenter = models.ForeignKey(Datacenter,null=True,blank=False)
   name = models.CharField(max_length=100, blank = False)
   type = models.CharField(max_length=50)

i'm trying to query it like:

obj_list = VM.objects.filter(d_store__datastore_id__type='rbd')

but i'm getting error that it is unable to resolve the field 'datastore_id'. Any ideas on how can i improve my query.

Upvotes: 0

Views: 86

Answers (1)

Eugene Soldatov
Eugene Soldatov

Reputation: 10135

Right variant is:

obj_list = VM.objects.filter(d_store__type='rbd')

Upvotes: 2

Related Questions