Defining Default Inline value for Django Admin forms? - django

If I have an inline admin class like this:
class ResourceInline(admin.TabularInline):
model = Resource
extra = 3
Is there any way to specify the default values for the 3 "extra" resources because the manual says that the prepopulated_fields doesn't accept ForeignKey fields.

You could override formfield_for_foreignkey and set the initial value of your fields.
Check this answer to get the basic idea: Default value for user ForeignKey with Django admin

You can also create default values for your inline extra models by targeting the parent admin class that uses ResourceInline as it's inline.
All you have to do is override the add_view function on the parent admin class:
Customize Django Admin: Add More Than One Default Inline on Parent Add_View

Related

how to change django model admin url?

I just need to change
http://localhost:8000/admin/song_management/album/
to
http://localhost:8000/admin/song-management/album/
Note:
I am using ModelAdmin & Changed admin template completely ( ex; Used hardcoded url mostly )
Django's ModelAdmin uses the model's app_label per default to generate the urls. The only way to bypass this would be to subclass AdminSite and change the relevant parts.

howto add custom form into django administrator page inlines

Can I get a form into Django Administrator page, that I have defined in forms.py?
Can I also get this form into Model inlines of Django Administrator page ?
To be clear, this is what I call inline:
class AnswerInline(admin.StackedInline):
...
Yeah, it's a bit complicated but the docs are actually clear here:
InlineModelAdmin.form
The value for form defaults to ModelForm. This is what is passed through to
inlineformset_factory when creating the formset for this inline.
So create your form class and then refer to it in the inline, like so:
class MyForm(forms.ModelForm):
…
class AnswerInLine(admin.StackedInline):
form = MyForm

How to change the default 3 instances showing in a InlineModelAdmin object in Django

When you define a InlineModelAdmin class in Django, by default it shows 3 models in the admin page of the model that has a Foreign Key to the model I defined in the class.
How can I change that?
I just don't want to use max_num field, because when using it, I won't be able to add another instances in the page.
Thanks
Instead of max_num use extra: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.extra

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)

Admin Form Integration for Custom Model Fields in Django

I need a model field composed of a numeric string for a Django app I'm working on and since one doesn't exist I need to roll my own. Now I understand how "get_db_prep_value" and such work, and how to extend the Model itself (the django documentation on custom model fields is an invaluable resource.), but for the life of me I can't seem to figure out how to make the admin interface error properly based on input constraints.
How do I make the associated form field in the admin error on incorrect input?
Have a look at the Form and field validation section in the Django documentation, maybe that's what you're looking for?
You would have to make a new type of form field for your custom model field.
All you need to do is define a custom modelform which uses your new field, and then tell the admin to use that form to edit your models.
class MyModelForm(forms.ModelForm):
myfield = MyCustomField()
class Meta:
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm