To allow multiple registrations to a model in Django Admin, you can follow these steps:
Here is some example code to help you get started:
# models.py
from django.db import models
class OriginalModel(models.Model):
name = models.CharField(max_length=100)
# other fields
class Registration(models.Model):
original_model = models.ForeignKey(OriginalModel, on_delete=models.CASCADE)
# other fields for registration data
# forms.py
from django import forms
from .models import Registration, OriginalModel
class RegistrationForm(forms.ModelForm):
original_model = forms.ModelChoiceField(queryset=OriginalModel.objects.all())
class Meta:
model = Registration
fields = '__all__'
# admin.py
from django.contrib import admin
from .models import OriginalModel, Registration
from .forms import RegistrationForm
class RegistrationAdmin(admin.ModelAdmin):
form = RegistrationForm
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj=None, **kwargs)
form.base_fields['original_model'].queryset = OriginalModel.objects.filter(id=obj.original_model.id) if obj else OriginalModel.objects.all()
return form
admin.site.register(OriginalModel)
admin.site.register(Registration, RegistrationAdmin)
This code allows users to register for an existing OriginalModel instance by selecting it from a dropdown field in the Registration Admin interface. When a user selects an OriginalModel instance, the RegistrationForm is displayed with the selected OriginalModel pre-populated in the field. When the user submits the form, a new Registration instance is created with a ForeignKey reference to the selected OriginalModel.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-04-29 06:33:50 +0000
Seen: 11 times
Last updated: Apr 29 '23
How can I change the Django Admin Password?
What is the process for incorporating std::hash into a class template?
What is the process for adding a class path permanently to the Mac terminal?
How can the __iter__ method of the cursor class in psycopg2 be expanded?
What is the process for obtaining and recording all Linq To Db queries in a database table?
What is the method for directing focus toward the subset within an existing class?
How do I reference media element events located on a ContentView from a ContentPage XAML CS class?
What is the issue when Selenium is unable to locate an element by its class name?
Is there a potential for a conflict between the name of a class and the name of a property?