Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3842110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:44:48+00:00 2026-05-19T15:44:48+00:00

Imagine that I have a model that describes the printers that an office has.

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:44:49+00:00Added an answer on May 19, 2026 at 3:44 pm

    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 }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a Domain assembly that describes the domain model, and
Imagine I have an object model: A Blog has many Articles, and an Article
Imagine that I have a form in a flash application with two fields, input1
Imagine that I have a nice Deck class, in the best OO fashion. It
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
imagine that I have a Form with 9 controls (TabbedStuffControl) in a 3x3 tile,
Let's imagine that I have to make a table with the following structure with
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
Let's imagine that I have a result of evaluating XPath expression //node/@*. MSXML6 returns
I would like to ask you: Imagine that you have a site and there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.