How to create private model field in Django? - django

Is there any way, to create a private field in Django. I want this field be accessible only from this class's methods.
Here is example of what do I want:
class TestModel(models.Model):
__some_field = models.CharField(max_length=255)
SOLUTION FOUND:
Update: finally, I forge my own solution for this case and added it into my django-fields package, which is available on the GitHub: https://github.com/svetlyak40wt/django-fields
Specifically, if you want to have private fields in your models, then you have to inherit them from my ModelWithPrivateFields class.
Example is available at django-fields's unittests.

Why would you want to do this?
If you just want to hide this field in admin interface you can use name of this field without leading underscore (it doesn't make any difference for Django, as far as I know) and register TestModelAdmin class:
class TestModelAdmin(admin.ModelAdmin):
exclude = ('some_field',)
admin.site.register(TestModel, TestModelAdmin)

Related

FeinCMS, intermediate model on content type field

I'm trying to accomplish the following:
class DownloadContentFiles(models.Model):
download_content = models.ForeignKey('DownloadContent')
media_file = models.ForeignKey(MediaFile)
class DownloadContent(models.Model):
files = models.ManyToManyField(MediaFile, through=DownloadContentFiles)
class Meta:
abstract=True
I can see why this doesn't work. Because of the abstract on DownloadContent.
Is there a workaround to specify a intermediate model for contenttype fields?
Generally, if you need more informations when creating a field (such as a list of choices) or a concrete Django model (as you do), you can use initialize_type for that.
class DownloadContent(models.Model):
#classmethod
def initialize_type(cls):
cls.add_to_class('files', ... your model field ...)
The MediaFileContent uses this method to add the type selector:
https://github.com/feincms/feincms/blob/master/feincms/content/medialibrary/models.py#L58
However, in your case this does not work because you'd also have to create the through model dynamically. The reason for that is that for each concrete DownloadContent, you'd need another concrete DownloadContentFiles model.
You could achieve this by using the type built-in to dynamically create new DownloadContentFiles concrete classes (beware of name clashes when using DownloadContent with different CMS bases such as page.Page and elephantblog.Entry).
Maybe a simpler way to achieve what you want:
Add a Downloads model somewhere, and add the files ManyToManyField to this class instead
The DownloadContent only contains a ForeignKey(Downloads)
Yes, you need another model. It might be worth it because you can build a better editing interface for Downloads, and the page editor is also simplified because you only have to select one of the already existing Downloads models for showing them on a page.
Maybe explicitely defining class_name to create_content_type could work for you. Something like this:
class DownloadContentFiles(models.Model):
download_content = models.ForeignKey('MyDownloadContent')
media_file = models.ForeignKey(MediaFile)
class DownloadContent(models.Model):
files = models.ManyToManyField(MediaFile, through=DownloadContentFiles)
class Meta:
abstract=True
Page.create_content_type(DownloadContent, class_name="MyDownloadContent")

Django: Want a ManyRelatedManager, getting ManyToManyField instead

So in my project I am trying to extend the User model to a Staff class and the Group model to a PermGroup class. However, when I save a PermGroup in the Staff's groups field (inherited from User) it only saves the PermGroup object as a Group and all of the fields and methods I defined in my PermGroup class are stripped away. So I decided the best course of action would be to override the groups field. From an earlier stackoverflow question I found and Django documentation, this should work.
class Staff(User):
User.groups = models.ManyToManyField('PermGroup', blank=True)
I need to use 'PermGroup' because the class shows up later in the file, and PermGroup has a field that relies on the Staff class, so if i switched the order I would have the same problem, only in the PermGroup class.
Now the problem I am having is that groups is now a ManyToManyField object where all the other "manytomany" fields are ManyRelatedManagers. I want groups to be a ManyRelatedManager but I do not know how.
Is it possible to get groups to be a ManyRelatedManager when I initiatize it using the 'PermGroup' model call?
If my approach is wrong and you can suggest an alternative to saving PermGroups in the Staff class. I would greatly appreciate it.
Why not just have your Staff be a standard model with a ForeignKey (OneToOneField, to be more exact) to his/her corresponding User?
And, to remove the circular dependency problem, you just need to make one dependent on the other. For instance, the PermGroup model could have a field of a ManytoMany of the Staff members in that group. There's no need for Staff to have a PermGroup, because if you wanted to see what groups a member belongs to, you'd just do something like this:
groups_theyre_in = PermGroups.objects.filter(staff_members__id=id_were_looking_for)

django: Admin interface for an extended class

I have two classes for which I want an admin interface. But one class extends the other. Ex
class Questions(models.Model):
pass
class MathQuestion(Questions):
some fields ....
Now the simplest way is to create a separate admin for MathQuestion. However, is there a possibility that I can have MathQuestion displayed in a more intuitive fashio in Admin, ex: When user goes to QuestionAdmin interface and selects a further type for Maths.
Add extra field 'question_type' with default value of None and not nullable (so you can be sure Question can't have instances, not sure if it works)
overload the save method in child class and assign there some value to question_type, meaning MathQuestion.
Use filtering in admin as always.

Django Model subclass without changing database name

I am making a Django Model decorator which takes a Django model and gives it a few extra methods.
Django creates the database name for this Model using: '%s_%s' % (app_name, class_name). When you decorate the Model the table name is suddenly derived from the app name and class name of the decorator rather than the original class (which is pythonically correct).
However I would like to maintain the original table name of the Model, is there a way to tell Django to use the super class to determin the database name, or a way to retrieve the table name and apply it in the model's Meta class.
You can override this in class Meta:
http://docs.djangoproject.com/en/1.1/ref/models/options/#django.db.models.Options.db_table
To make a new model using the specs of the superclass, look into proxy = True
http://docs.djangoproject.com/en/1.1/topics/db/models/#id8

Django Show M2M field in either model when using forms.ModelForm

I am using the forms.ModelForm to create my form. I want to be able to show the manytomany field in both model forms, how do I do this?
If the manytomany relationship is defined in the model it is fine and just appears but if it is not in the model (but is still linked via the other model) it does not appear. How can I make it show up?
Hope this makes sense.
Thanks
Use this third party model field class. It's a four-liner that subclasses the regular ManyToMany class, but instructs Django not to create a separate table for the second relationship.
You create the relationship on the first model normally, explicitly specifying database table name ("db_table" option):
class FirstModel(models.Model):
second_model = ManyToManyField('SecondModel', related_name='second_model', db_table=u'TABLE_FOR_FIRST_AND_SECOND_MODEL')
...
And for the second model use ManyToManyField_NoSyncdb, so it doesn't try to create a second table:
class SecondModel(models.Model):
first_model = ManyToManyField_NoSyncdb('FirstModel', related_name='first_model', db_table=u'TABLE_FOR_FIRST_AND_SECOND_MODEL')
...
For more information refer to the right-hand description on django snippets.