我试图在某个实例以特定方式自动创建的模型中创建一个字段。下面是相关代码:在Django模型中以某种格式创建自动ID
models.py
class Student(models.Model):
# This first field is what I'm focusing on
student_id = models.CharField(max_length=128, unique=True)
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
ssn = USSocialSecurityNumberField(null=False)
gender = models.CharField(max_length=128, choices=GENDER_CHOICES)
dob = models.DateField(auto_now=False, auto_now_add=False, db_column="date of birth")
address = models.CharField(max_length=128)
city = models.CharField(max_length=128)
state = USStateField(choices=STATE_CHOICES, default='NJ')
zipcode = USZipCodeField(blank=True)
country = CountryField(default='US', blank=True)
home_phone = models.CharField(max_length=128)
cell_phone = models.CharField(max_length=128)
email = models.EmailField(max_length=254, validators=[validate_email])
background = models.CharField(max_length=128, choices=BACKGROUND_CHOICES)
# This field is relevant
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
workforce = models.CharField(max_length=128, choices=WORKFORCE_CHOICES, default='--')
source = models.CharField(max_length=128, choices=SOURCE_CHOICES, default='individual')
refer_by = models.CharField(max_length=128, choices=REFER_BY_CHOICES, default='no refer')
last_status = models.CharField(max_length=128, choices=LAST_STATUS_CHOICES, default='followup')
newsletter = models.BooleanField()
created_by = models.CharField(max_length=128)
# This field is relevant
date = models.DateField(auto_now=False, auto_now_add=False)
notes = models.TextField()
我想使它在一定的格式自动创建,使student_id数据领域的不同:
AASSSYYMMDD
AA为“ AV',当学生没有付款时,'PS'。我现在默认为'PS',所以不用担心。
SSS是大写的位置的前三个字母
YYMMDD是年,月,日场的一天的最后两个数字分别
我听说我应该使用AutoSlugField或SlugField,但我遇到的每个示例只填充一个字段的全部内容,当我想将多个字段的某些部分放在一起时将其填充。有谁知道如何正确地从不同的领域采取适当的格式?谢谢。
编辑:下面是一些其他代码:
forms.py
class StudentForm(forms.ModelForm):
# STEP 1 FORM
# student_id = forms.CharField(max_length=128, label="Student ID")
# first_name = forms.CharField(max_length=128, label="First Name", widget=forms.TextInput(attrs={'class': 'form-control'}))
# last_name = forms.CharField(max_length=128, label="Last Name")
ssn = USSocialSecurityNumberField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="SSN", help_text="Format: xxx-xx-xxxx")
# gender = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Gender", choices=GENDER_CHOICES)
dob = forms.DateField(input_formats=['%m/%d/%Y'], widget=DateInput(format='%m/%d/%Y'), label="Date of birth", help_text="Format: mm/dd/yyyy")
# contact_number = forms.CharField(max_length=128, label="Contact number")
# address = forms.CharField(max_length=128, label="Address")
# city = forms.CharField(max_length=128, label="City")
# state = forms.ChoiceField(choices=STATE_CHOICES, initial="NJ", label="State")
# zipcode = USZipCodeField(label="Zipcode")
# country = forms.ChoiceField(choices=countries, label="Country", initial="US")
home_phone = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control bfh-phone', 'data-format': '+1 (ddd) ddd-dddd'}), max_length=128, label="Home phone")
cell_phone = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control bfh-phone', 'data-format': '+1 (ddd) ddd-dddd'}), max_length=128, label="Cell phone")
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}), max_length=254, validators=[validate_email], label="Email")
# background = forms.ChoiceField(choices=BACKGROUND_CHOICES, label="Background")
# location = forms.ChoiceField(choices=LOCATION_CHOICES, initial="south_plainfield", label="Location")
# workforce = forms.ChoiceField(choices=WORKFORCE_CHOICES, initial="--", label="Workforce")
# source = forms.ChoiceField(choices=SOURCE_CHOICES, initial="individual", label="Source")
# refer_by = forms.ChoiceField(choices=REFER_BY_CHOICES, initial="no refer", label="Refer by")
# last_status = forms.ChoiceField(choices=LAST_STATUS_CHOICES, initial="followup", label="Last status")
newsletter = forms.BooleanField(widget=forms.CheckboxInput(), label="Newsletter", required=False)
# created_by = forms.CharField(max_length=128, label="Created by")
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control'}), label="Date", help_text="Format: yyyy-mm-dd")
notes = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control'}), required=False, label="notes", help_text="less than 1000 characters")
class Meta:
model = Student
fields = ('first_name', 'last_name', 'ssn', 'gender', 'dob', 'address', 'city', 'state', 'zipcode', 'country', 'home_phone', 'cell_phone', 'email', 'background', 'location', 'workforce', 'source', 'refer_by', 'last_status', 'newsletter', 'created_by', 'date', 'notes')
views.py
def add_Student(request):
form = StudentForm()
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
form.save(commit=True)
return render(request, 'students/add_student_success.html', {})
else:
print(form.errors)
return render(request, 'students/add_student.html', {'form': form})
student_id字段将如何看待模型本身?这与模型有关吗? –
您保持原样,不需要更改当前模型结构中有关student_id字段的内容。 –
此代码是否应该放在模型内部或外部?关于字符串,我将如何从日期字段中提取数字?这就是我到目前为止想到的:'PV'+ instance.location [:3] .upper()+,但是你把日期放入。还有,它是动态的吗?说我想改变位置。这也会改变学生ID吗? –