Telephone.objects.filter(contact__nickname='jill')
See documentation when creating queries
what if i wanted to be able to edit jill and give her more than one number via
different saves is this possible?
, . , : "--" ForeignKey "--". .
, .
, Jill , . , , "" "", :
jill = Contact.objects.get(nickname='jill') # assuming you only have one jill
new_num = Telephone.objects.create(contact=jill,number=5551212)
, Jill, :
call_jill = Telephone.objects.filter(contact=jill)
call_jill = Telephone.objects.filter(contact__nickname='jill')
ManyToMany:
class Telephone(models.Model):
number = models.PhoneNumberField()
class Contact(models.Model):
phones = models.ManyToMany(Telephone)
:
jills_phones = Contact.objects.get(nickname='jill').phones.all()
:
jill = Contact.objects.get(nickname='jill')
jill.phones.add(Telephone.objects.create(number=5551212))
jill.save()
, , :
phone = Telephone.objects.get(number=5551212)
whose_is_it = phone.contact_set.all()
?
, . . , - .
, . Pizza ManyToMany Toppings.
, , ForeignKey. , :
, , . , , . .
, . , (- ForeignKey).
, , . , , :
class Pizza(models.Model):
topping = models.ForeignKey('Topping')
class Topping(models.Model):
name = models.CharField(max_length=100)
class AssembledPizza(models.Model):
topping = models.ForeignKey('Topping')
pizza = models.ForeignKey('Pizza')
, ManyToMany, :
class Pizza(models.Model):
toppings = models.ManyToMany('Topping')
class Topping(models.Model):
name = models.CharField(max_length=100)
:
, . . create.
veggie = Pizza()
veggie.toppings.add(Topping.objects.create(name='Olives'))
veggie.toppings.add(Topping.objects.create(name='Peppers'))
veggie.save()
, .