Adding help_text in a superclass attribute - python

Let's say I have a model called MySuper:
class MySuper(models.Model):
some_attr = models.CharField(max_length=128)
And I have two subclasses that inherit from this model, called MySub1 and MySub2.
I need to add a help_text to some_attr, but must be different in each subclass. Is there any way to do this?
class MySub1(MySuper):
# ...
# add help_text='Help text of some_attr inside MySub1'
class MySub2(MySuper):
# ...
# add help_text='Help text of some_attr inside MySub2'

Another way to achieve this without having to fully redefine the field would be:
class MySub1(MySuper):
pass
MySub1._meta.get_field('some_attr').help_text = 'sub1 help text'
class MySub2(MySuper):
pass
MySub2._meta.get_field('some_attr').help_text = 'sub2 help text'

I needed to do this for two identical models that have different image sizes. There's probably some brilliant way to generalise this, but for two items my answer will do fine. First create a file called forms.py in your app.
from django import forms
from .models import FeaturedProduct, ShopProduct
class FeaturedProductForm(forms.ModelForm):
class Meta:
model = FeaturedProduct
ihelp = "Image should be 500x220."
src = forms.ImageField(help_text=ihelp,required=False)
class ShopProductForm(forms.ModelForm):
class Meta:
model = FeaturedProduct
ihelp = "Image should be 100x100."
src = forms.ImageField(help_text=ihelp,required=False)
Note that required defaults to true, even if you have null=True, blank=True on your models. Then in admin.py define your ModelAdmins as follows:
from .forms import FeaturedProductForm, ShopProductForm
class ShopProductAdmin(admin.ModelAdmin):
form = ShopProductForm
class FeaturedProductAdmin(admin.ModelAdmin):
form = FeaturedProductForm
I left out a few imports and the whole admin.site.register nonsense. Let me know if you need any more info. The complete list of forms.FIELDS can be found here:
https://docs.djangoproject.com/en/dev/ref/forms/fields/

Related

How to set a variable from one class equals to a variable in another class in Django models.py?

I am a new in Django world and I want to link two classes from models.py so that i can set their variables equal to each other. Here is the models.py code:
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class file(models.Model):
title = models.CharField(max_length=250)
FILE_TYPE_CHOICES = (
('audio','Audio'),
('games','Games'),
('videos','Videos'),
('applications','Applications'),
('books','Books/Docs'),
('others','Others')
)
file_type = models.CharField(max_length=10,choices=FILE_TYPE_CHOICES,default='others')
description = models.TextField(max_length=6000)
#uploader_username = ???
def get_absolute_url(self):
return reverse('one:user')
def __str__(self):
return self.title
class user (models.Model):
username= models.CharField(max_length=100)
email=models.EmailField
password= models.CharField(max_length = 100)
user_files = models.ForeignKey(file, on_delete=models.CASCADE)
Here I want to set uploader_username from file class equals tousername from user class.
No, you don't want to do this. You want a ForeignKey from File to User, not the other way round, then you can just access my_file.user.username.
Note, it is a bad idea to define your own user class like this. There can be good reasons for doing so, but if so you must inherit from the abstract base classes in the auth app; failure to do so is a serious security problem as you will be storing passwords in clear text. It doesn't look like you need your own model here; you should remove this class.

In a Django admin, add an inline of a generic relation

Here are my simplified models :
from django.contrib.contenttypes.fields import (
GenericForeignKey, GenericRelation)
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Thing(models.Model):
'''
Our 'Thing' class
with a link (generic relationship) to an abstract config
'''
name = models.CharField(
max_length=128, blank=True,
verbose_name=_(u'Name of my thing'))
# Link to our configs
config_content_type = models.ForeignKey(
ContentType,
null=True,
blank=True)
config_object_id = models.PositiveIntegerField(
null=True,
blank=True)
config_object = GenericForeignKey(
'config_content_type',
'config_object_id')
class Config(models.Model):
'''
Base class for custom Configs
'''
class Meta:
abstract = True
name = models.CharField(
max_length=128, blank=True,
verbose_name=_(u'Config Name'))
thing = GenericRelation(
Thing,
related_query_name='config')
class FirstConfig(Config):
pass
class SecondConfig(Config):
pass
And Here's the admin:
from django.contrib import admin
from .models import FirstConfig, SecondConfig, Thing
class FirstConfigInline(admin.StackedInline):
model = FirstConfig
class SecondConfigInline(admin.StackedInline):
model = SecondConfig
class ThingAdmin(admin.ModelAdmin):
model = Thing
def get_inline_instances(self, request, obj=None):
'''
Returns our Thing Config inline
'''
if obj is not None:
m_name = obj.config_object._meta.model_name
if m_name == "firstconfig":
return [FirstConfigInline(self.model, self.admin_site), ]
elif m_name == "secondconfig":
return [SecondConfigInline(self.model, self.admin_site), ]
return []
admin.site.register(Thing, ThingAdmin)
So far, I've a Thing object with a FirstConfig object linked together.
The code is simplified: in an unrelevant part I manage to create my abstract Config at a Thing creation and set the right content_type / object_id.
Now I'd like to see this FirstConfig instance as an inline (FirstConfigInline) in my ThingAdmin.
I tried with the django.contrib.contenttypes.admin.GenericStackedInline, though it does not work with my current models setup.
I tried to play around with the fk_name parameter of my FirstConfigInline.
Also, as you can see, I tried to play around with the 'thing' GenericRelation attribute on my Config Model, without success..
Any idea on how to proceed to correctly setup the admin?
According to the Django Docs you have to define the ct_fk_field and the ct_field if they were changed from the default values. So it may be enough to set ct_field to config_content_type.
Hope it works!
edit: Those values have to be declared in the Inline:
class SecondConfigInline(admin.StackedInline):
model = SecondConfig
ct_fk_field = "config_object_id"
ct_field = "config_content_type"
edit2:
I just realized an error in my assumption. Usually you should declare the Foreignkey on the Inline-model. Depending on the rest of your code you could just remove the generic Foreignkey on Thing+the genericRelation on Config and declare a normal Foreignkey on the Config-Basemodel.
This question is old, but I'll give it a try anyway.
I think the solution depends on what kind of relation you intend to create between Thing and your Config subclasses.
many-to-one/one-to-many
The way it is currently set up, it looks like a many-to-one relation: each Thing points to a single Config subclass, and many Things can point to the same Config subclass. Due to the generic relation, each Thing can point to a different model (not necessarily a Config subclass, unless you do some extra work).
In this case I guess it would make more sense to put the inline on the admin for the Config. That is, create a GenericStackedInline for Thing (which has the GenericForeignkey), and add the inline to a ConfigAdmin, which you can then use for all Config subclasses. Also see the example below. The generic inline will then automatically set the correct content_type and object_id.
many-to-many
On the other hand, if you are looking for a many-to-many relation between Thing and each Config subclass, then I would move the GenericForeignkey into a separate many-to-many table (lets call it ThingConfigRelation).
A bit of code says more than a thousand words, so let's split up your Thing class as follows:
class Thing(models.Model):
name = models.CharField(max_length=128)
class ThingConfigRelation(models.Model):
thing = models.ForeignKey(to=Thing, on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType, null=True, blank=True,
on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(null=True, blank=True)
config_object = GenericForeignKey(ct_field='content_type',
fk_field='object_id')
Now it does make sense to add an inline to the ThingAdmin. The following is a bare-bones example of an admin that works for both sides of the relation:
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericStackedInline
from .models import Thing, FirstConfig, SecondConfig, ThingConfigRelation
class ConventionalTCRInline(admin.StackedInline):
model = ThingConfigRelation
extra = 0
class GenericTCRInline(GenericStackedInline):
model = ThingConfigRelation
extra = 0
class ThingAdmin(admin.ModelAdmin):
inlines = [ConventionalTCRInline]
class ConfigAdmin(admin.ModelAdmin):
inlines = [GenericTCRInline]
admin.site.register(Thing, ThingAdmin)
admin.site.register(FirstConfig, ConfigAdmin)
admin.site.register(SecondConfig, ConfigAdmin)
Note that we use the conventional inline for the ForeignKey-side of the relation (i.e. in ThingAdmin), and we use the generic inline for the GenericForeignKey-side (in ConfigAdmin).
A tricky bit would be filtering the content_type and object_id fields on the ThingAdmin.
... something completely different:
Another option might be to get rid of the GenericForeignKey altogether and use some kind of single-table inheritance implementation with plain old ForeignKeys instead, a bit like this.

How can i remove extra "s" from django admin panel?

I am really very much irritated by the extra "s" added after my class name in django admin eg class 'About' in my model.py becomes 'Abouts' in admin section. And i want it not to add extra 's'. Here is my model.py file-
class About(models.Model):
about_desc = models.TextField(max_length=5000)
def __unicode__(self): # __str__ on Python 3
return str(self.about_desc)
Please anybody suggest me how django can solve my problem.
You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category, the admin displays Categorys, but by adding the Meta class, we can change it to Categories.
I have changed your code to fix the issue:
class About(models.Model):
about_desc = models.TextField(max_length=5000)
def __unicode__(self): # __str__ on Python 3
return str(self.about_desc)
class Meta:
verbose_name_plural = "about"
For more Meta options, refer to https://docs.djangoproject.com/en/1.8/ref/models/options/
Take a look at the Model Meta in the django documentation.
Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming.
This can be used in the following way (in english we do not have sheeps) so verbose_name_plural can be used to override djangos attempt at pluralising words:
class Sheep(model.Model):
class Meta:
verbose_name_plural = 'Sheep'
inside model.py or inside your customized model file add class meta within a Model Class.
If not mentioned then a extra 's' will be added at the end of Model Class Name which will be visible in Django Admin Page.
class TestRoles(model.Model):
class Meta: verbose_name_plural = 'TestRoles'

Multiple Django Admin Arguments with Extensions

Is there a way to use multiple Django extensions in the admin.site.register() inside admin.py? I'm using "simple-history" and "import-export" extensions, but I can only have one of them in the admin.site.register().
Example: I have a model named, "Cars", that is using the "simple-history" extension so I need admin.site.register(Cars, SimpleHistoryAdmin), as their documentation says it should. I want to use the import/export extension as well to the same "Cars" model, but the admin.site.register() doesn't take multiple arguments for me to add it.
models.py
class Cars(models.Model):
Year = models.CharField(max_length=30)
Make = models.CharField(max_length=30)
Model = models.CharField(max_length=30)
history = HistoricalRecords()
class Meta:
verbose_name_plural = "Car Table"
def __str__(self):
return self.Make
admin.py
class CarResource(resources.ModelResource):
class Meta:
model = Cars
fields = ('id','Year', 'Make', 'Model',)
class CarAdmin(ImportExportModelAdmin):
resource_class = CarResource
pass
#I want to use the import/export extension (code above), along with simple-history
admin.site.register(Cars, CarAdmin)
admin.site.register(Cars, SimpleHistoryAdmin)
I've tried using a proxy and inlines, but the proxy makes a new model which I don't want and when using inlines I get an error saying that it requires a foreign key, but I'm not trying to get the model objects from a different model. Naming them the same model doesn't work because the model is already registered. Any help is much appreciated!
In python, class can have more than one parent. Just inherit from 2 parents at once. But both ImportExportModelAdmin and SimpleHistoryAdmin are inheriting from ModelAdmin, that's not good. There is also ImportExportMixin, we can use it instead of ImportExportModelAdmin, so there will be only one reference to ModelAdmin.
class CarResource(resources.ModelResource):
class Meta:
model = Cars
fields = ('id','Year', 'Make', 'Model',)
class CarAdmin(ImportExportMixin, SimpleHistoryAdmin):
resource_class = CarResource
pass
#I want to use the import/export extension (code above), along with simple-history
admin.site.register(Cars, CarAdmin)

Django how to filter queryset by a subclass using django model utils?

I am using django-model-utils for inheritance Managers. I want to get results of only one subclass at a time.
managers.py
from model_utils.managers import InheritanceManager
class PostManager(InheritanceManager):
pass
models.py
from .managers import PostManager
class Post(models.Model):
title = models.CharField(max_length=20)
text = models.TextField()
objects = PostManager()
class ImagePost(Post, models.Model):
source = models.URLField()
image = models.ImageField(upload_to="images/%Y/%m/%d")
class VideoPost(Post, models.Model):
source = models.URLField()
I want to return results of only image type. by writing a simpler query like this.
Post.objects.filter(type='image').select_subclasses()
What i have tried:
if type == 'image':
Post.objects.filter(imagepost__isnull=False).select_subclasses()
This works but is kind of anti-pattern, i don't want to write conditions in views for every content type.
Is there better way like defining a property in models or converting it into a manager method? or am i missing something?
Have you tried to pass the class to select_subclasses method?
Post.objects.select_subclasses(ImagePost)
Check their doc about this feature.
Edit:
I misunderstood the question, but sounds like OP wants only the Post with type ImagePost. Doing select_subclasses(ImagePost) would fetch everything and convert the objects with type ImagePost to ImagePost instances. The solution should be as simple as :
image_posts = ImagePost.objects.all()

Categories

Resources