I have a model with lots of say CharField fields, that I would like to edit in the admin.
The problem is that each field takes up one line. How should I make them display like this (horizontally):
(source: djangoproject.com)
(they are not foreign keys)
Django 1.2 has ListEdit extension for the Admin
This is how you use it:
class AccountAdmin(admin.ModelAdmin):
list_display = ( 'Name','Type')
list_editable = ( 'Type', )
And this is how it looks like:
(source: v-lad.org)
Related
I want to customize my Django Admin Search form.
Like this picture, I want to add select box to choose search fields.
Is there any built in parameter in Django? or Is there other way to make this possible?
enter image description here
I think you have to add search_fields to your model admin class, like below:
# admin.py
class MyModelAdmin(ModelAdmin):
...
search_fields = (
'field1',
'field2',
'field3',
)
This way the search will only apply to these fields of your model.
So i work in a company and we constantly need to add view fields for the user, I would like to know if there is a way to make this option available to the end user, for example a dropdown with the options that the model admin where he is allowing.
Today we use the django way of doing admin, for example:
list_display = (
'course',
'class',
'discipline',
'teacher',
'start_date',
'end_date'
)
I don't know if this what you are looking for, but give a try: How can I dynamically specify the "list_display" attribute of a django ModelAdmin class?
A kind regard
Emilio
In the classical example of Articles/Publications, when I create a Publication record in Admin page, I would like to be able to add existing Articles to that Publication. How can I do that?
I tried following:
class PublicationAdmin(admin.ModelAdmin):
articles = Article.objects.all()
admin.site.register(Publication, PublicationAdmin)
though I do not see any articles to select at all.
I can do the other way around, when I add a new article, I can select a publication by adding
class ArticleAdmin(admin.ModelAdmin):
fieldsets =[('Publications', {'fields': ['publications']})]
You need to define an InlineModelAdmin on the through model, in your case the through model is Publication.article_set.through. See the docs.
I have two models, Product and Category and ManytoMany field in Product.
The Category appear as key on ProductCreate View.
I need to customize the widget and field for Categories.
I checked in Django source Fields and Widgets but I don't see a reference(class) for ManyToMany.
To what type of Field and Widget ManyToMany relationship corresponds(I presume is Charfield as save or SelectField)? Where I can find the code ? (an example to customize field/widget in this case)
A model ManyToManyField is represented as a MultipleChoiceField and the default widget is SelectMultiple But, we can customise it.
You can find it in below references.
[1]https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#field-types
[2]https://docs.djangoproject.com/en/dev/ref/forms/widgets/#setting-arguments-for-widgets
from django import forms
from . import models
class ProductForm(forms.ModelForm):
class Meta:
model = models.Post
fields = [<fields-for-your-product-form>]
categories = forms.ModelMultipleChoiceField(
queryset=models.Category.objects.all(),
widget=forms.CheckboxSelectMultiple
)
This will render checkboxes in your form but you can substitute it with your preferred widget.
I've done something similiar where I was adding tags to blog posts. I wrote a tutorial for it- goes into more detail: https://ctrlzblog.com/how-to-add-tags-to-your-blog-a-django-manytomanyfield-example/
I'm using Django and I have created several models in models.py. Some fields are optional and some fields are mandatory.
Is a there a way that a user can view only mandatory fields in forms?
I'm using crispy forms package to render my forms. Now the user can see all the fields from the models.
Thank you in advance!
U can get all model's data (looking for blank=True ? ) by parsing from your model
MODELNAME._meta.get_fields_with_model()
or
MODELNAME._meta.__dict__
run over fields and make alist of fields u wanna exclude from the form .
next - exclude fields
From the docs
django proj
class PartialAuthorForm(ModelForm):
class Meta:
model = Author
exclude = ['title']
or what ever is good for u