Django drop downs in model -


i'm providing choices in dropdown in model this:

class foodtype(models.model):   type = models.charfield(max_length=30, unique=true)    def __unicode__(self):     return self.type  class food(models.model):   name = models.charfield(max_length=30, unique=true)   type = models.foreignkey(foodtype)    def __unicode__(self):     return self.name 

i did rather hardcoded choices because want provide option add/delete/change foodtypes via admin once app deployed. realised once if foodtype deleted food dependent on, food deleted, don't want. want able keep food records unless explicitly want delete one.

is there better way still allows user modify foodtypes via admin?

thanks :)

you can set on_delete parameter of foreignkey field value different cascade (the default value).

ex:

type = models.foreignkey(foodtype, null=true, on_delete=models.set_null) 

Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -