Adding a Simple Custom Field to Django - How to Write Southern Introspection Rules

I am trying to add a custom field to a Django project that uses South . Because of this, I am trying (for the first time) to write introspection rules for the South . I find my case to be the simplest as I am simply extending CharField. In particular:

class ColorField(models.CharField): def __init__(self, *args, **kwargs): kwargs['max_length'] = 10 super(ColorField, self).__init__(*args, **kwargs) def formfield(self, **kwargs): kwargs['widget'] = ColorPickerWidget return super(ColorField, self).formfield(**kwargs) 

This is from a Django fragment called the jQuery color picker model field for those interested.

Since I am not adding any new attributes, I believe that I need to add these lines of code:

 from south.modelsinspector import add_introspection_rules add_introspection_rules([], ["^myproject\.myapp\.models\.ColorField"]) 

This is probably obvious, but where should they go? Also, I suppose that is all I have to do right?

I looked at a few of the questions posted here, but most of them relate to much more complex introspections.

Per http://south.readthedocs.org/en/latest/customfields.html#where-to-put-the-code , I tried puttin code at the top of my models.py file, where a custom field is defined. But that did not work.

+6
source share
2 answers

The simple answer is yes, the code should go in the models.py file where the field was defined. The correct code is:

 from south.modelsinspector import add_introspection_rules add_introspection_rules([], ["^myapp\.models\.ColorField"]) 

I don’t know why I posted the name of the project there.

+7
source

You must make sure the file path is correct. The one you mention is similar to the one I use, but the way is:

 add_introspection_rules([], ["^colors\.fields\.ColorField"]) 
+1
source

Source: https://habr.com/ru/post/921258/


All Articles