Same Options For Different Django Admin Class - python

This should evident but I dont know how to do it.
How do I Apply the same options for instance a exclude for different model and different admin class
-- admin.py
class Tabla1(admin.ModelAdmin):
exclude('tenant')
...
class Tabla2(admin.ModelAdmin):
exclude('tenant')
...
class Tabla3(admin.ModelAdmin):
exclude('tenant')
...
That I want is exclude the same tenant field in the tables. I have the same field in several tables. In fact I have several actions(same actions) to do for the different admin models.

Just make a base admin class that you can inherit from:
class TablaBaseAdmin(admin.ModelAdmin):
class Meta:
exclude = ('tenant',)
class Tabla1Admin(TablaBaseAdmin):
pass
class Tabla2Admin(TablaBaseAdmin):
pass
class Tabla3Admin(TablaBaseAdmin):
pass

Related

Make models fields readonly in all admin pages

My project has multiple Models and custom admin pages for the models. All the Models inherit from a "BaseModel". For business functionality, we had to update our "Base model" to include 2 new fields. Given that all models inherit these 2 new fields, they are now showing up in admin pages as editable fields. As per business functionality, these two fields should be displayed as read-only fields
For making fields readonly we normally use readonly_fields = [read only..] in admin class. Is there a way to achieve this without touching all the admin classes?
Create a base admin model class and use it on every admin model class. AFAIK, it will be the best solution you can have
from django.contrib import admin
class BaseModelAdmin:
"""
The base model admin class
"""
readonly_fields = ["field_1", "field_2"]
class FooModelAdmin(BaseModelAdmin, admin.ModelAdmin):
"""
Inheriting the 'BaseModelAdmin' class here
"""
...
class BarModelAdmin(BaseModelAdmin, admin.ModelAdmin):
"""
Inheriting the 'BaseModelAdmin' class here
"""
def get_readonly_fields(self, request, obj=None):
"""
If you have "readonly_fields" that are specific to certain Model, override this method
"""
readonly_fields = super().get_readonly_fields(request, obj=None) + ["bar_field_1", "bar_field_2"]
return readonly_fields
admin.site.register(FooModel, FooModelAdmin) # registering the model in Django admin
admin.site.register(BarModel, BarModelAdmin) # registering the model in Django admin

how to modify admin page to add extra columns?

can anybody explain how to add more columns in django admin page.
and how the meta class works, how it adds the columns from list
class SignUpAdmin(admin.ModelAdmin): # what is ModelAdmin class
list_display = ['__unicode__','timestamp'] # columns created in models file
class Meta:
model = SignUp # SignUp class created as Model class in model.py file
admin.site.register(Signenter code hereUp, SignIpAdmin)
you don't need to use Meta class in your admin class. when you want to add database columns to your admin UI just pass them in your list_display=['title', 'timestamp',] array

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'

Django model derived class

i' ve a django model with some fields and methods like this:
class Follow(models.Model):
fields = ...
def methods(self, ...):
...
. I' d like to create another model with the same fields and methods, however in this new model i' d like to have new fields and methods as well, like this:
class Auto(Follow):
additionalfields = ...
def additionalmethods(self, ...):
...
, but in this case the problem is that if i create an Auto object, it' ll appear in the Follow.objects.filter() QuerySet as well. How could i workaround it? An idea was to specify an is_auto = models.BooleanField(default = ?) field in both of the models with relevant value, but that fails during the schemamigration:
django.core.exceptions.FieldError: Local field 'autob' in class 'Auto' clashes
with field of similar name from base class 'Follow'
. Any idea how to workaround it?
class BaseItem(models.Model):
#fields and methods
class Follow(BaseItem):
pass
class Auto(BaseItem):
#additional fields

Standard way to pull a specific model from a common view in Django/DRF?

I currently have two models that are essentially the same (inheriting from a base model), but I have problems referring to them from a common view:
Models:
class BaseModel(models.Model):
name = models.CharField(...)
owner = ForeignKey(...)
class Cat(BaseModel):
...
class Dog(BaseModel):
...
View:
class CommonViewset(viewsets.ModelViewSet):
#link()
def set_owner(self, request, pk=None):
#how do I get Cat or Dog models cleanly here?
#super fugly/unstable way
url_path = request.META['PATH_INFO']
if 'cats' in url_path:
Cat.objects.get(pk=pk).owner = ...
elif 'dogs' in url_path:
Dog.objects.get(pk=pk).owner = ...
I can also put the set_owner link in separate views, but that feels un-DRY. Thanks in advance for looking into this!
You can pass the model to use in the as_view method of your class:
url(r'^cats/my-url/$', CommonViewSet.as_view(model=Cat)),
The ModelViewSet class inherits from Django's View class, so this will set the model attribute on the instance of your viewset. You can then use self.model to get the right model for the current url.

Categories

Resources