Do properties work on Django model fields?

Question:

I think the best way to ask this question is with some code… can I do this:

class MyModel(models.Model):    
    foo = models.CharField(max_length = 20)    
    bar = models.CharField(max_length = 20)  

    def get_foo(self):  
        if self.bar:  
            return self.bar  
        else:  
            return self.foo  

    def set_foo(self, input):  
        self.foo = input  

    foo = property(get_foo, set_foo)  

or do I have to do it like this:

class MyModel(models.Model):
    _foo = models.CharField(max_length = 20, db_column='foo')
    bar = models.CharField(max_length = 20)

    def get_foo(self):
        if self.bar:
            return self.bar
        else:
            return self._foo

    def set_foo(self, input):
        self._foo = input

    foo = property(get_foo, set_foo)

note: you can keep the column name as ‘foo’ in the database by passing a db_column to the model field. This is very helpful when you are working on an existing system and you don’t want to have to do db migrations for no reason

Asked By: Jiaaro

||

Answers:

A model field is already property, so I would say you have to do it the second way to avoid a name clash.

When you define foo = property(..) it actually overrides the foo = models.. line, so that field will no longer be accessible.

You will need to use a different name for the property and the field. In fact, if you do it the way you have it in example #1 you will get an infinite loop when you try and access the property as it now tries to return itself.

EDIT: Perhaps you should also consider not using _foo as a field name, but rather foo, and then define another name for your property because properties cannot be used in QuerySet, so you’ll need to use the actual field names when you do a filter for example.

Answered By: Andre Miller

As mentioned, a correct alternative to implementing your own django.db.models.Field class, one should use the db_column argument and a custom (or hidden) class attribute. I am just rewriting the code in the edit by @Jiaaro following more strict conventions for OOP in python (e.g. if _foo should be actually hidden):

class MyModel(models.Model):
    __foo = models.CharField(max_length = 20, db_column='foo')
    bar = models.CharField(max_length = 20)

    @property
    def foo(self):
        if self.bar:
            return self.bar
        else:
            return self.__foo

    @foo.setter
    def foo(self, value):
        self.__foo = value

__foo will be resolved into _MyModel__foo (as seen by dir(..)) thus hidden (private). Note that this form also permits using of @property decorator which would be ultimately a nicer way to write readable code.

Again, django will create _MyModel table with two fields foo and bar.

Answered By: Yauhen Yakimovich

The previous solutions suffer because @property causes problems in admin, and .filter(_foo).

A better solution would be to override setattr except that this can cause problems initializing the ORM object from the DB. However, there is a trick to get around this, and it’s universal.

class MyModel(models.Model):
    foo = models.CharField(max_length = 20)
    bar = models.CharField(max_length = 20)

    def __setattr__(self, attrname, val):
        setter_func = 'setter_' + attrname
        if attrname in self.__dict__ and callable(getattr(self, setter_func, None)):
            super(MyModel, self).__setattr__(attrname, getattr(self, setter_func)(val))
        else:
            super(MyModel, self).__setattr__(attrname, val)

    def setter_foo(self, val):
        return val.upper()

The secret is ‘attrname in self.__dict__‘. When the model initializes either from new or hydrated from the __dict__!

Answered By: Justin Alexander

It depends whether your property is a means-to-an-end or an end in itself.

If you want this kind of “override” (or “fallback”) behavior when filtering querysets (without first having to evaluate them), I don’t think properties can do the trick. As far as I know, Python properties do not work at the database level, so they cannot be used in queryset filters. Note that you can use _foo in the filter (instead of foo), as it represents an actual table column, but then the override logic from your get_foo() won’t apply.

However, if your use-case allows it, the Coalesce() class from django.db.models.functions (docs) might help.

Coalesce() … Accepts a list of at least two field names or
expressions and returns the first non-null value (note that an empty
string is not considered a null value). …

This implies that you can specify bar as an override for foo using Coalesce('bar','foo'). This returns bar, unless bar is null, in which case it returns foo. Same as your get_foo() (except it doesn’t work for empty strings), but on the database level.

The question that remains is how to implement this.

If you don’t use it in a lot of places, simply annotating the queryset may be easiest. Using your example, without the property stuff:

class MyModel(models.Model):
    foo = models.CharField(max_length = 20)
    bar = models.CharField(max_length = 20)

Then make your query like this:

from django.db.models.functions import Coalesce

queryset = MyModel.objects.annotate(bar_otherwise_foo=Coalesce('bar', 'foo'))

Now the items in your queryset have the magic attribute bar_otherwise_foo, which can be filtered on, e.g. queryset.filter(bar_otherwise_foo='what I want'), or it can be used directly on an instance, e.g. print(queryset.all()[0].bar_otherwise_foo)

The resulting SQL query from queryset.query shows that Coalesce() indeed works at the database level:

SELECT "myapp_mymodel"."id", "myapp_mymodel"."foo", "myapp_mymodel"."bar",
    COALESCE("myapp_mymodel"."bar", "myapp_mymodel"."foo") AS "bar_otherwise_foo" 
FROM "myapp_mymodel"

Note: you could also call your model field _foo then foo=Coalesce('bar', '_foo'), etc. It would be tempting to use foo=Coalesce('bar', 'foo'), but that raises a ValueError: The annotation 'foo' conflicts with a field on the model.

There must be several ways to create a DRY implementation, for example writing a custom lookup, or a custom(ized) Manager.

A custom manager is easily implemented as follows (see example in docs):

class MyModelManager(models.Manager):
    """ standard manager with customized initial queryset """
    def get_queryset(self):
        return super(MyModelManager, self).get_queryset().annotate(
            bar_otherwise_foo=Coalesce('bar', 'foo'))


class MyModel(models.Model):
    objects = MyModelManager()
    foo = models.CharField(max_length = 20)
    bar = models.CharField(max_length = 20)

Now every queryset for MyModel will automatically have the bar_otherwise_foo annotation, which can be used as described above.

Note, however, that e.g. updating bar on an instance will not update the annotation, because that was made on the queryset. The queryset will need to be re-evaluated first, e.g. by getting the updated instance from the queryset.

Perhaps a combination of a custom manager with annotation and a Python property could be used to get the best of both worlds (example at CodeReview).

Answered By: djvg
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.