exclude in ModelAdmin vs ModelForm - django

Is the exclude list in a ModelForm any different from an exclude list in a ModelAdmin? If my ModelForm is tied to the ModelAdmin, where do I need to specify the exclude list ideally; in the ModelForm Meta class or in the ModelAdmin subclass?

Depends if you want to do something else with the form. If its displayed somewhere else than the admin and you want to exclude the same field there as well, define it in the ModelForm. If the ModelForm is just used in the admin and nowhere else you basically can choose what makes more sense for you. I personally would still keep it in the ModelForm so this functionality is tied to it instead to the admin.
Edit (see comments below):
Apparently there seems to be a bug in Django. If I exclude something in a ModelForm and then use this form in the ModelAdmin it is still showing this field for some reason. You better exclude in the admin only to be 100% sure or specify fields in the ModelForm without the field you want to exclude.

Related

Can anyone please explain me the what are Django modelform_factory and modelformset_factory?

Kindly explain me the what are Django modelform_factory and modelformset_factory, and how do they correlate to the Django models, forms and modelforms?
I'm a beginner to Django and came across these terms. Looked up on the internet but wasn't able to find any good source of information on these.
Thank you.
modelform_factory is a method to create forms in views , without defining form in forms.py file.
There are multiple places, where you have automatically generated forms or formsets, the most visible is admin and Generic Class Views.
Every time you are working with HTML forms do add or modify data - you should use forms.Form. If you are working with the model - there is forms.ModelForm which is build on top of the Form - handling automatically declaration of fields, some validations and instance save. Then on top of this you have modelform_factory - which is generating on-demand ModelForm - so it is kind of shortcut to define default ModelForm for your model.
Similar, modelformset_factory is generating ModelFormSet to be used with one-to-many relations.

Create multiple objects in django admin at once

For example, I have a Post model:
Class Post(models.Model):
title = models.Charfield(max_length=200)
# other fields
I wonder is there a way to create multiple posts at once in admin. In other words, I need a formset instead of single form on post creation page.
I've heard recently about a django app that exactly does this job. It's called django-bulk-admin and enables bulk add/update in the admin.
Possibly, the best way to do exactly what you want is extend the ModelAdmin class, because it has no formsets on it, except for those used on InlineFormsets.
After that you could customize the admin change_form template, to include your formsets
The quick-and-dirty way to do it using admin is wrap your Post model as an inline formset of another modeladmin and add the extra option to it.

UpdateView with a model which includes Taggit field

I can create an object from a specific model with the generic class based view CreateView, which has a Taggit field. I also have a ModelForm based in that model. I insert many taggs separated with a comma, without any problem. I also can update it properly from my admin interface.
The problem comes, when I update the model with the generic UpdateView which is based in a ModelForm of that model. My Taggit field appears like that in the textfield:
[<TaggedItem: tag1>, <TaggedItem: tag2>, <TaggedItem: tag3>]
How can I show it, as the same way as in the admin interface?
which in this case, would be:
tag1,tag2,tag3

Django represent a many-to-many relationship as a CharField

I have a many-to-many field on one of my models and a ModelForm to represent it. I have it in a template but it shows up as a multiple select field. I need it to show up as a CharField so the user can put in comma-delimited values. Is there any way to do this?
Take a look at the code in django.contrib.admin.widgets.ForeignKeyRawIdWidget to see how the admin's raw_id_fields are implemented and try specifying it as the widgets= kwarg when you define that field on your form.

Change field in Django Flatpages Admin

Using Flatpages with the default admin, I need to change the template field from a text input with to select or radio with predefined choices. It's easy to do this with one of my own apps - just use the choices attribute in the model.
I have tried a few things - I will add details about those attempts later if necessary - but does anyone know a nice way to do this?
Define a custom flatpages ModelAdmin class which inherits from the default one but uses a custom form. On this form, override the field, using the widget you want. Then unregister the flatpages admin and reregister it with your custom class.
from django.contrib.flatpages.admin import FlatPageAdmin, FlatpageForm
class MyFlatpageForm(FlatpageForm):
template = forms.ChoiceField(choices=MY_CHOICES)
class MyFlatPageAdmin(FlatPageAdmin):
form = MyFlatpageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MyFlatPageAdmin)