-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-fill talk with publication info #1200
Comments
Maybe some possibilities here. Two approaches: (1) use GET by pasing in variables to Talk; (2) using get_form https://stackoverflow.com/questions/529890/django-how-to-prepopulate-admin-form-fields Gemini suggets get_form. I want to figure this out without adding a different talk button like Gemini suggests. Here's how you can achieve your goal of pre-populating the Talk form fields with data from the Publication in the Django admin interface: 1. Modify the Instead of opening a blank Talk admin form, we will customize the from django.contrib import admin
from .models import Talk, Publication
class TalkAdmin(admin.ModelAdmin):
# Pre-populate specific fields based on the selected Publication
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'publication':
kwargs['queryset'] = Publication.objects.filter(pk=request.GET.get('publication_id'))
return super().formfield_for_foreignkey(db_field, request, **kwargs)
def formfield_for_manytomanyfield(self, db_field, request, **kwargs):
# This can be used to pre-populate authors field if needed
# ...
return super().formfield_for_manytomanyfield(db_field, request, **kwargs)
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
# Access the selected publication through request.GET
publication = Publication.objects.get(pk=request.GET.get('publication_id'))
if publication:
# Pre-populate specific fields
form.initial['title'] = publication.book_title
form.initial['date'] = publication.date
form.initial['forum_name'] = publication.forum_name
form.initial['forum_url'] = publication.forum_url
form.initial['location'] = publication.location
# ... (pre-populate other relevant fields)
return form
admin.site.register(Talk, TalkAdmin) Explanation:
2. Modify the Publication admin interface: In the Publication admin form, we need to modify the from django.urls import reverse
class PublicationAdmin(admin.ModelAdmin):
# ... other fields
talk = admin.ForeignKeyModelChoiceField(
'talk',
on_delete=models.DO_NOTHING,
blank=True,
null=True,
related_name='publications',
)
def talk_link(self, obj):
if obj.talk_id:
url = reverse('admin:your_app_name_talk_change', args=[obj.talk_id])
return f'<a href="{url}">Edit Talk</a>'
return '-'
talk_link.short_description = 'Talk'
talk_link.allow_tags = True
# ... other methods
admin.site.register(Publication, PublicationAdmin) Explanation:
Remember:
This approach allows you to pre-populate the Talk form with data from the selected Publication in the Django admin interface, making the process more efficient and consistent. |
OK, my solution is quite fun and interesting combining some basic JavaScript that passes the pub ID via GET param to the talk admin page. And then we have the following in TalkAdmin
|
Reopening until we test on -test server |
When you add a talk or video from the publication admin interface, we should auto-fill the info.
The text was updated successfully, but these errors were encountered: