There is a model whose field is unique and may be null. But the problem is that these two concepts seem to contradict each other: if I try to make two fields null, then I get an error, because there is already a field with a null value. How to be? For example:

class TestModel(models.Model): identifier = models.CharField(unique=True, blank=True, null=True) 

PS Ie I would like the uniqueness not to apply to the case when set to null.

Python 2.7, Django 1.8

What else is interesting: when I add the first empty record in my database, not an empty string is displayed, but some two apostrophes ''. It is not clear where they come from. There is nothing like this in the code. And at the same time, this field does not appear in the admin panel of this model and, accordingly, one cannot even switch to this model. But if I add an empty line to the database in this field, then I’ll write "(Nothing)" in the admin panel and I can go there.

Found one seemingly working version here . But another problem arose - now there is nothing in the admin panel at the place of this field (for those lines where it is empty). How to set the text displayed when the value is empty? As default, "(Nothing)" was displayed.

enter image description here

  • Please your version of django - Mae
  • Added to post. - Yalierar
  • I added both entries normally with default = None. Django 1.9.4 - Mae
  • Well, I don’t know if something has changed there compared to 1.8. I do not have the opportunity to upgrade. By the way, by googling this problem as it should be, I realized that it is actually not just me ... As an example, the link I gave above or here for example (by the way, the same code is one to one). - Yalierar
  • The display in the admin - you need to manually configure, there are several methods to taste. By the way, in django 1.9 there is also a special method for the empty value: empty_value_display - Mae

0