Link to home
Start Free TrialLog in
Avatar of MK15
MK15

asked on

Django - queryset containing foreign key reference

Hi,

How would i go about getting all the results from 2 tables that match foreign key to primary key?

SQL:- select * from company_room, company_property where company_room.property_id = company_property.id

Model.py
class Property(models.Model):
  street = models.CharField(max_length=100, blank=False)
  city = models.CharField(max_length=100, blank=False)

class Room(models.Model):
  room_number = models.IntegerField(blank=True)
  property = models.ForeignKey(Property) 

Open in new window


Forms.py

class RoomsForm(forms.ModelForm):
    selection = ModelChoiceField(queryset=Room.objects.all())

    class Meta:
        model = Room
        fields = "__all__"

Open in new window


Thanks in advance,
MP
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial