I am thinking of implementing the function of saving the text that users entered in Django in DB and calling out the choices from DB when registering new data.I think there is no problem with setting up models.py and setting up forms.py, but when I actually tested the new registration in my browser, the initial value may not be set.Error "Please select correctly.The selected item is not in the candidate list and cannot be saved to DB.How can I pass the initial choice to DB?
models.py
import datetime
from django.db import models
from django.conf import settings
ITEM_TYPE_CHOICES=[('jacket', 'top', ('shirt', 'shirt', ('pants', 'pants', 'underpants', 'underpants', 'underpants', 'underpants', ('underpants', 'underpants', 'underpants', 'underpants', 'underpants', (top)'), ('socks', 'socks', ('), ('others', 'others')]]
ITEM_COLOR_CHOICES = [('red', 'red', ('blue', 'blue', ('green', 'green', ('yellow', 'yellow', ('purple', 'purple', 'orange', 'black', 'white', 'white', ('grey', 'ash', ('', 'beige)'), beige, navy
SEASON_CHOICES=[('spring', 'spring', ('summer', 'summer', ('fall', 'autumn'), ('winter', 'winter')]
OCCASION_CHOICES=[('daily_use', 'daily_use', 'work', 'work', ('active_wear', 'outside'), ('sports_wear', 'sports', ('other_use', 'other')]]
FAVORITE_LEVEL_CHOICES = [(1, 'very low', (2, 'low', (3, 'normal', (4, 'high', (5, 'very high')]
ITEM_IMPORTANCE_CHOICES=[(1, 'I can throw it away',(2, 'I'm worried',(3, 'Normal',(4, 'It's important',(5, 'It's very important')]
classItemType(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
create_date=models.DateTimeField(verbose_name='CreateDate', auto_now_add=True)
item_type=models.CharField(verbose_name='item type', max_length=155, unique=True)
class ItemColor (models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
create_date=models.DateTimeField(verbose_name='CreateDate', auto_now_add=True)
item_color=models.CharField(verbose_name='item color', max_length=100, unique=True)
class ItemBrand (models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
create_date=models.DateTimeField(verbose_name='CreateDate', auto_now_add=True)
item_brand=models.CharField (verbose_name='brand', max_length=155, unique=True)
class PurchasePlace(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
create_date=models.DateTimeField(verbose_name='CreateDate', auto_now_add=True)
purchase_place=models.CharField(verbose_name='Where to Buy', max_length=155, unique=True)
classCloset(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
closeet_name=models.CharField(verbose_name='closet name', max_length=255)
closeet_memo=models.CharField(verbose_name='closet memo', max_length=325)
create_date=models.DateTimeField(verbose_name='Closet Creation Date', auto_now_add=True)
class Meta:
ordering = ['create_date']
def__str_(self):
return self.closet_name
Class Item (models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='user', on_delete=models.CASCADE, null=True)
item_type=models.ForeignKey(ItemType, verbose_name='itemType', on_delete=models.CASCADE, null=True,blank=True,db_column='item_type')
item_color=models.ForeignKey(ItemColor, verbose_name='item color', on_delete=models.CASCADE, blank=True, null=True, db_column='item_color')
item_brand=models.ForeignKey(ItemBrand, verbose_name='Brand Name', on_delete=models.CASCADE, blank=True, null=True, db_column='item_brand')
purchase_place=models.ForeignKey(PurchasePlace, verbose_name='Purchase Location', on_delete=models.CASCADE, blank=True, null=True, db_column='purchase_place')
item_name=models.CharField(verbose_name='item name', max_length=300)
purchase_date=models.DateTimeField(verbose_name='Purchase Date', blank=True, null=True)
pricing=models.IntegerField(verbose_name='Purchase Price (yen),)
item_image=models.ImageField(verbose_name='item image', upload_to=', blank=True, null=True)
memo=models.TextField(verbose_name='note', blank=True, null=True)
create_date=models.DateTimeField(verbose_name='item registration date', auto_now_add=True)
update_date=models.DateTimeField(verbose_name='item update date', auto_now=True)
closeet=models.ForeignKey(Closet, on_delete=models.PROTECT, blank=True, null=True, default=None)
season=models.CharField(verbose_name='season', max_length=10, choices=SEASON_CHOICES, default='spring')
occasion=models.CharField(verbose_name='scene', max_length=30, choices=OCCASION_CHOICES, default='daily_use')
favorite_level=models.IntegerField(verbose_name='Favorite', choices=FAVORITE_LEVEL_CHOICES, blank=True, null=True, default='1')
item_importance=models.IntegerField(verbose_name='Importance', choices=ITEM_IMPORTANCE_CHOICES, blank=True, null=True, default='1')
class Meta:
ordering = ['item_name']
def__str__(self):
return self.item_name
forms.py
from django import forms
from django.conf import settings
from.models import ItemType, ItemColor, ItemBrand, PurchasePlace, Closeet, Item, FAVORITE_LEVEL_CHOICES, ITEM_IMPORTANCE_CHOICES, SEASON_CHOICES, OCCASION_CHOICES
MONTHS = {
1: 'January', 2: 'February', 3: 'March', 4: 'April',
5: 'May', 6: 'June', 7: 'July', 8: 'August',
9: 'September', 10: 'October', 11: 'November', 12: 'December'
}
classClosetForm(forms.ModelForm):
class Meta:
model=Closet
fields=('closet_name', 'closet_memo')
labels = {'closet_name': 'Closet name', 'closet_memo': 'Closet memo'}
classItemForm(forms.ModelForm):
item_type=forms.ModelChoiceField(queryset=ItemType.objects.all(), label='ItemType', empty_label='Select', widget=forms.TextInput)
item_color=forms.ModelChoiceField(queryset=ItemColor.objects.all(), label='ItemColor', empty_label='Select', initial=', widget=forms.TextInput)
item_brand=forms.ModelChoiceField(queryset=ItemBrand.objects.all(), label='Brand', empty_label='Select', initial=', widget=forms.TextInput)
purchase_place=forms.ModelChoiceField(queryset=PurchasePlace.objects.all(), label='Where to Buy', empty_label='Select', initial=', widget=forms.TextInput)
class Meta:
model=Item
fields=('item_type', 'item_color', 'item_brand', 'purchase_place', 'item_name', 'purchase_date', 'pricing', 'item_image',
'memo', 'closet', 'season', 'occasion', 'favorite_level', 'item_importance')
labels={ 'item_type': 'item type', 'item_color': 'item color', 'item_brand': 'brand', 'purchase_place': 'place of purchase', 'item_name': 'item name',
'purchase_date': 'Purchase Date', 'pricing': 'Purchase Price (yen), 'item_image': 'item image', 'memo': 'note', 'closet': 'closet', 'season', 'occasion': 'scene',
'favorite_level': 'Favorite', 'item_importance': 'Important'}
widget = {
'season': forms.RadioSelect(choices=SEASON_CHOICES),
'occasion': forms.RadioSelect(choices=OCCASION_CHOICES),
'purchase_date': forms.SelectDateWidget(years=[x for x in range (2000, 2040)], months=MONTHS),
'favorite_level': forms.RadioSelect(choices=FAVORITE_LEVEL_CHOICES),
'item_importance': forms.RadioSelect(choices=ITEM_IMPORTANCE_CHOICES),
}
Django choices.How to set default option?
I think this is a similar question.
It seems that you can do it using the default field.
When I searched for keywords like the following, it became a hit.
angdjango choice model initial value / or
ddjango choice model default <
© 2024 OneMinuteCode. All rights reserved.