Django, extra argument for model field definition

2019-08-06 05:24发布

问题:

I wanted to add extra argument to model.Field base class to use later without defining field's extra options again in Form or ModelForm class.

And I can add extra argument with:

class Poll(models.Model):

    question = models.CharField(max_length=200,
                                extra= { 'widget': 'xw', 
                                         'admin_list_order': 3 })

    pub_date = models.DateTimeField('date published')

I can access extra arg with:

(InteractiveConsole)
>>> from polls.models import Poll
>>> Poll._meta.fields[1].extra
{'widget': 'xw', 'admin_list_order': 3}
>>> 

Is this an appropriate, Pythonic way to add an extra argument?

from django.db import models

# save old __init__ function of Field class
__oInit = models.Field.__init__

# define a new one to accept 'extra' arg
def __xinit__(self, *args, **kwargs):

    #chek if extra arg exist
    if kwargs.has_key('extra'):
        self.extra = kwargs.pop('extra')

    #call real __init__ 
    __oInit(self, *args, **kwargs)

# replace init with new one
models.Field.__init__ = __xinit__

回答1:

This could break if they ever add an extra argument to a field. And monkeypatches are discouraged in general. You could go a safer route by adding a method to the Field base class:

def set_extra(self, **kwargs):
    self.extra = kwargs
    return self
models.Field.set_extra = set_extra

Then define your models like this:

class Poll(models.Model):
    question = models.CharField(max_length=200).set_extra(
            widget='xw', admin_list_order=3)

has_key is considered unpythonic, preferred way to check for key presence is:

if 'extra' in kwargs:
    self.extra = kwargs.pop('extra')

More pythonic would be just trying it and catching failure:

try:
    self.extra = kwargs.pop('extra')
except KeyError:
    pass