django: custom name for model in admin site - django

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'

Related

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.

Simple ForeignKey in ClassBasedView in django

I have a model with a foreignkey to another model
class Person(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
class Organisation(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
contact = models.ForeignKey(Person)
I want to use a CreateView to be able to create a new Organisation, but be able to enter a new contact person details on the same page (i.e. when a new organisation is created, a new contact person must also be created).
What is the nicest (DRY) way to do this?
In the CreateView use the model that has the ForeignKey and since it inherits the FormMixin's form_class use the modelform_factory for that model with extra fields the fields of ForeignKey's model. Finally, overload either the validation or save methods with a get_or_create with the ForeignKey's model fields, passing the result to the ModelForm.
An alternative approach would be to chain two CreateViews. First with the Organization as the model, using the Contact's CreateView URL as its success_url. You can even use js to replace the first submit with the html of the second view.
Or you can try some hacks floating around utilizing formsets, though I prefer the first two methods in your case. The formset hacks are better suited for many-to-many relationships.

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 admin: search for foreign key objects rather than <select>?

My model looks like this:
class Asset(models.Model):
serial_number = models.CharField(max_length=100, unique=True)
asset_tag = models.CharField(max_length=100, unique=True)
class WorkOrder(models.Model):
asset = models.ForeignKey(Asset)
Essentially, a work order is submitted and then an admin assigns an asset to the work order. The asset_tag field is a barcode that we can scan in. When editing the work order in the Django admin, by default the asset field is displayed as a <select> widget. What we want to be able to do is have a search field so that we can scan the asset tag and then search for the right asset in the DB to associate with the work order.
I know you can customize the Django admin foreign key to a hard coded query, but I can't figure out how to get it so it does a search based on a field on the admin page.
Did you take a look at raw_id_fields?
It should be pretty to close to what you're after.
If you are using Django >= 2.0, you can take advantage of a feature called autocomplete_fields. You must define search_fields on the related object’s ModelAdmin because the autocomplete search uses it.
Since you have a ForeignKey relationship to Asset in WorkOrder, in the admin.py of your app add the following:
from django.contrib import admin
#admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
search_fields = ["serial_number", "asset_tag"]
#admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
autocomplete_fields = ["asset"]
Add the fields you want to use for searching to search_fields, and add define autocomplete_fields as shown in the code above.
Now you can use the autocomplete_fields from django 2.0.
It's quite neat.

Django fix Admin plural

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"