How to create a private model field in Django?

Is there a way to create a private field in Django. I want this field to be available only from these class methods.

Here is an example of what I want:

class TestModel(models.Model): __some_field = models.CharField(max_length=255) 

SOLUTION FOUND:

Update: Finally, I create my own solution for this case and added it to my django-fields package, which is available on GitHub: https://github.com/svetlyak40wt/django-fields

In particular, if you want to have private fields in your models, you must inherit them from the ModelWithPrivateFields class.

An example is available in django-fields unittests .

+4
source share
1 answer

Why do you need this?

If you just want to hide this field in the admin interface, you can use the name of this field without underscore (for Django, as far as I know, it does not matter) and register the TestModelAdmin class:

 class TestModelAdmin(admin.ModelAdmin): exclude = ('some_field',) admin.site.register(TestModel, TestModelAdmin) 
0
source

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


All Articles