I have the following model:
class Product(models.Model):
provinces = models.ManyToManyField('Province', related_name='formats')
By default, products can be sold in every province. How can I define the model "Product" so that every product created has all provinces by default?
Thanks!
Use the default key. You can't directly set default model values to an iterable like a list, so wrap them in a callable, as the Django documentation advises: https://docs.djangoproject.com/en/1.8/ref/models/fields/
def allProvinces():
return provincesList
provinces = models.ManyToManyField('Province', related_name='formats', default=allProvinces)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With