Django fix Admin plural - django

How do I change some models name from "Categorys" to "Categories" on admin site in the new dev django version?
In the old version (whithout admin sites and admin models) you could just do this;
http://www.the-dig.com/blog/post/customize-plural-name-django-admin/
However - now setting verbose_name_plural inside my modeladmin based class does nothing.
Anyone encouter the same issue?

Well well, it seems like the Meta class approach still works.
So placing a meta class inside your model will still do the trick:
class Category(models.Model):
class Meta:
verbose_name_plural = "categories"
Note that we use the lower case here, as django is smart enough to capitalize it when we need it.
I find setting this option in model-class weird as opposed to the admin.py file.
Here is the location in the dev docs where it is described:
http://docs.djangoproject.com/en/dev/ref/models/options/#verbose-name-plural

for that you need to add meta classes for models
class Category(models.Model):
--- model field here ---
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
Bonus for your models admin in apps.py
class CategoryConfig(AppConfig):
name = "Category"
verbose_name = "Categories"

Related

Using Djongo's ArrayField at Django Admin

Djongo is a Django and MongoDB database connector witch brings, beside others, the ArrayField to Django Models. It allows one to store multiple instances of an other djongo.models.Model in an unique MongoDB Array field inside a document related with the Model that has the Arrayfield
As described in source documentation:
"The model of the container must be declared as abstract, thus should not be treated as a collection of its own."
As well as in djongo's site tutorials
"In case you don’t plan on using your embedded model as a standalone model (which means it will always be embedded inside a parent model) you should add the class Meta and abstract = True This way Djongo will never register this model as an actual model."
This way I made:
# models.py
class Contact(models.Model):
key = models.CharField(max_length=100)
class Meta:
abstract = True
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ('key',)
class Person(models.Model):
_id = models.ObjectIdField()
...
contact = models.ArrayField(
model_container=Contact,
model_form_class=ContactForm,
)
objects = models.DjongoManager()
# admin.py
admin.site.register(Person)
$ pip freeze
Django==3.2.4
djongo==1.3.6
pymongo==3.11.4
...
However when I try to add a Person through Django Admin (in /admin/<app>/person/add/) I receive the error Abstract models cannot be instantiated from .../django/db/models/base.py
This seems inconsistent with Djongo's description. Am I doing something wrong?

django restframework: blank=False and default='x' creates a modelsearializer with required=False field. how to circumvent?

See example below, that will create a serializer with a not required testing field (obviously, as there is a default). I know I can add the field manually to the serializer, with required=True. But, having many fields, I would like to have DRF do the work for me. Or I could remove the default on the modelfield. Dont want this either...is there a thing, like required_fields, on the Meta, perhaps? Or another workaround?
Model
class MyModel(models.Model):
testing = models.CharField(max_length=3, blank=False, default='x')
Serializer
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
The reason why I need this: Having a vue.js CRUD like app, that is creating new instances...without specifying the initial fields on the object.

Hiding a model field in Django admin 1.9

I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`

Admin Site appending letter "s" to end of each model\table name on Django

I tried the verbose name when registering it on my admin.py so that it would appear as Data instead of Datas but that did not work.
admin.site.register(Data, verbose_name="Data")
Any ideas?
You should be setting verbose_name_plural in that case. Docs here.
Also you should be setting it on the model's Meta options of your model (docs here). Example:
class MyModel(models.Model):
# my fields
class Meta:
verbose_name_plural = "PluralForMyModel"

django: custom name for model in admin site

I have an app which includes a model "QuesTags". Now when I create an entry for this model in admin.py, the admin displays this model as "Ques tagss", which IMHO is totally unpalatable. Is there a way around( ex. short_description ) to display a custom string instead of parsing the original model name?
Figured the way out. The model needed meta class option "verbose_name_plural"/"verbose_name". Google has all the answers, provided you know what you are searching for :).
You can change the name in admin.py using meta class:
class QuesTags(models.Model):
class Meta:
verbose_name = 'QuesTag'
verbose_name_plural = 'QuesTags'