Ask Your Question
0

How can Django Admin be customized to allow multiple registrations to a model?

asked 2023-04-29 06:33:50 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-29 06:49:01 +0000

woof gravatar image

To allow multiple registrations to a model in Django Admin, you can follow these steps:

  1. Create a custom model for the registration data. This model should have a ForeignKey field to the original model that you want to allow multiple registrations for.
  2. Create a custom ModelForm for the registration data model. The form should include a dropdown field to select the original model instance that the user wants to register for.
  3. Create a custom Admin class for the registration data model. In the admin class, you can override the get_form method to pass the selected original model instance to the form.
  4. Register the custom admin class for the registration data model in the Django Admin site.

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-04-29 06:33:50 +0000

Seen: 11 times

Last updated: Apr 29 '23