How to register the same data multiple times with one input on the Django management screen

Thing you want to do

I want to create my own form on the additional screen so that I can register almost the same number as I entered. I want to separate the form on the add screen and the change screen

manner

In this example, multiple items with the same serial number but different branch numbers can be registered at one time.

Model

models.py


from django.db import models

class Job(models.Model):
	serial_number	=	models.IntegerField("Serial number")
	branch_number	=	models.IntegerField("Branch number")
	deadline		=	models.DateField("The deadline")

Admin

admin.py


from django.contrib import admin
from django import forms
from .models import Job

#Form at the time of addition
class JobAdminAddForm(forms.ModelForm):
	#Original form
	number_of_jobs	=	forms.IntegerField(lable="Number of work")
	
	class Meta:
		model = Job
		fields = ('serial_number', 'deadline')

#Form at the time of change
class JobAdminChangeForm(forms.ModelForm):
	class Meta:
		model = Job
		fields = ('serial_number', 'branch_number', 'deadline')
		

class JobAdmin(admin.ModelAdmin):
	list_display = ('id', 'serial_number', 'branch_number', 'deadline')
	
	#Form switching
	def get_form(self, request, obj=None, **kwargs):
		if obj:#Specify JobAdminChangeForm as the form at the time of change
			self.form = JobAdminChangeForm
		else:#Specify JobAdminAddForm as the form when adding
			self.form = JobAdminAddForm
		return super().get_form(request, obj, **kwargs)
	
	def save_model(self, request, obj, form, change):
		#Save as is when changing
		if change:
			obj.save()
		else:
			for i in range(form.cleaned_data['number_of_jobs']):
				obj.pk	=	None
				obj.branch_number = i + 1
				obj.save()
				
admin.site.register(Job, JobAdmin)

Override get_form to switch between forms when adding and when changing. By overriding save_model, the process at the time of saving can be rewritten. When changing, just save it as it is, and when adding, turn the for statement with the value of the original form and add the value to the branch number and save it. By substituting None for obj.pk, you can save each as separate data.

It seems that various things can be done by devising save_model. Please point out if there is anything.

Recommended Posts

How to register the same data multiple times with one input on the Django management screen
How to make only one data register on the Django admin screen
Transit to the update screen with the Django a tag
How to filter foreign keys that can be selected on the Django admin screen
How to embed multiple embeds in one message with Discord.py
How to check ORM behavior in one file with django
How to register on pypi
[Django] How to give input values in advance with ModelForm
DJango Memo: From the beginning (more edits to the management screen)
[Introduction to Python] How to get data with the listdir function
How to send a file in one shot by connecting to a host on the other side of the platform with SCP in multiple stages
How to return the data contained in django model in json format and map it on leaflet
[It's ridiculous to set the setting value to zero!] I receive the same message multiple times with SQS.
How to handle multiple versions of CUDA in the same environment
How to deal with "You have multiple authentication backends configured ..." (Django)
[Django] Correct the plural form of the model name on the management screen
[Django] Display registration data associated with users on the registration / edit form (Form)
The story of failing to update "calendar.day_abbr" on the admin screen of django
How to deal with imbalanced data
How to get started with Django
How to Data Augmentation with PyTorch
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to store CSV data in Amazon Kinesis Streams with standard input
TLE seemed to be scary depending on how the input was received
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
Plot multiple maps and data at the same time with Python's matplotlib
How to do arithmetic with Django template
How to title multiple figures with matplotlib
How to check the version of Django
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
How to read problem data with paiza
Useful for changing permissions on Linux! How to count up to 31 with one hand.
[2015/11/19] How to register a service locally using the python SDK on naoqi os
Script to use multiple github accounts properly in the same repository on the same machine
How to get the key on Amazon S3 with Boto 3, implementation example, notes
How to automatically generate API document with Django REST framework & POST from document screen
[Python] How to save images on the Web at once with Beautiful Soup
I'm addicted to the difference in how Flask and Django receive JSON data
Checklist on how to avoid turning the elements of numpy's array with for
How to add pre-save processing when adding objects on the Django admin site
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
[Flask + Keras] How to infer multiple models at high speed on the server
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to read original data or external data on the Internet with scikit-learn instead of attached data set such as iris
How to assign multiple values to the Matplotlib colorbar
How to create sample CSV data with hypothesis
The easiest way to get started with Django
How to specify the NIC to scan with amazon-dash
Save multiple models in one form with Django
Recreate the django staff registration screen with class
How to develop a cart app with Django
I tried to save the data with discord
[Django] How to get data by specifying SQL.
How to try the friends-of-friends algorithm with pyfof
How to scrape horse racing data with BeautifulSoup
How to print debug messages to the Django console
How to implement "named_scope" of RubyOnRails with Django
Building multiple Python environments on the same system
How to install OpenGM on OSX with macports