Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to make an unique, blank models.CharField?

Imagine that I have a model that describes the printers that an office has. They could be ready to work or not (maybe in the storage area or it has been bought but not still in th office ...). The model must have a field that represents the phisicaly location of the printer ("Secretary's office", "Reception", ... ). There cannot be two repeated locations and if it is not working it should not have a location.

I want to have a list in which all printers appear and for each one to have the locations where it is (if it has). Something like this:

ID | Location
1  | "Secretary's office"
2  |
3  | "Reception"
4  | 

With this I can know that there are two printers that are working (1 and 3), and others off line (2 and 4).

The first approach for the model, should be something like this:

class Printer(models.Model):
      brand = models.CharField( ...
      ...
      location = models.CharField( max_length=100, unique=True, blank=True )

But this doesn't work properly. You can only store one register with one blank location. It is stored as an empty string in the database and it doesn't allow you to insert more than one time (the database says that there is another empty string for that field). If you add to this the "null=True" parameter, it behaves in the same way. This is beacuse, instead of inserting NULL value in the corresponding column, the default value is an empty string.

Searching in the web I have found http://www.maniacmartin.com/2010/12/21/unique-nullable-charfields-django/, that trys to resolve the problem in differnt ways. He says that probably the cleanest is the last one, in which he subclass the CharField class and override some methods to store different values in the database. Here is the code:

from django.db import models
class NullableCharField(models.CharField):
     description = "CharField that obeys null=True"
     def to_python(self, value):
         if isinstance(value, models.CharField):
             return value
         return value or ""

     def get_db_prep_value(self, value):
         return value or None

This works fine. You can store multiple registers with no location, because instead of inserting an empty string, it stores a NULL. The problem of this is that it shows the blank locations with Nones instead of empty string.

ID | Location
1  | "Secretary's office"
2  | None
3  | "Reception"
4  | None

I supposed that there is a method (or multiple) in which must be specify how the data must be converted, between the model and the database class manager in the two ways (database to model and model to database).

Is this the best way to have an unique, blank CharField?

Thanks,

like image 582
jaimefma Avatar asked Nov 05 '22 03:11

jaimefma


1 Answers

You can use a model method to output the values in a custom way.

Like this (in your model class):

def location_output(self):
    "Returns location and replaces None values with an empty string"
    if self.location:
        return self.location
    else:
        return ""

Then you can use it in views like this.

>>> Printer.objects.create(location="Location 1")
<Printer: Printer object>
>>> Printer.objects.create(location=None)
<Printer: Printer object>
>>> Printer.objects.get(id=1).location_output()
u'Location 1'
>>> Printer.objects.get(id=2).location_output()
''

And in your templates, like this.

{{ printer.location_output }}
like image 175
Arseny Avatar answered Dec 07 '22 23:12

Arseny