B
B
bituke2020-10-25 16:51:38
Django
bituke, 2020-10-25 16:51:38

How to make an optional foreignkey association in django?

There is an Item model, which, if desired , can suddenly start referring to the Category_item model (many-to-one relationship, if I'm not mistaken),
models.py:

class Category_item(models.Model):
  name_category = models.CharField('Название категории', max_length=100)

class Item(models.Model):
  name = models.CharField('Название пункта', max_length=100)
  description = models.TextField('Описание пункта')
  Category_item = models.ForeignKey(Category_item, on_delete=models.CASCADE)


The problem is that the Item model must belong to some category, how can I fix this?
I need the model to be independent, until I want the opposite.

I'm sorry to bother you with this question, I can not formulate the question in Google, due to not knowing what it's called.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-10-25
@bituke

https://docs.djangoproject.com/en/3.1/ref/models/f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question