0
쿼리로 장고 양식을 만들려고하는데 잘못된 방법으로 계속 수행합니다. 몇 가지 예를 살펴 보았지만 약간 다른 예를 들었습니다. 여기에 내 코드,Django 폼을 미리 채우는 중
르 양식
class ItemForm(ModelForm):
class Meta:
model = Item
exclude = ('deleted')
과 shopping_list
라인이 정말 많은 오류를 선도 뷰
def index(request):
user = User
try:
last_modified_list = ShoppingList.objects.filter(deleted='0').filter(owner=user).latest('date_modified')
items = Item.objects.filter(shopping_list=last_modified_list).filter(deleted='0')
except ObjectDoesNotExist:
items = Item.objects.filter(deleted='0')
last_used_currency = ExtendedUser.objects.filter(owner=user)
#currency = forms.CharField(initial=last_used_currency.last_currency)
try:
last_used_shoppinglist = ShoppingList.objects.filter(deleted='0').filter(owner=user).latest('date_modified')
except ObjectDoesNotExist:
last_used_shoppinglist = datetime.datetime.now()
item_form = ItemForm (request.POST or None)
item_form.fields["shopping_list"]=last_used_shoppinglist
if item_form.is_valid():
name = item_form.cleaned_data['name']
bought = item_form.cleaned_data['bought']
currency = item_form.cleaned_data['currency']
price = item_form.cleaned_data['price']
date_added = item_form.cleaned_data['date_added']
date_modified = item_form.cleaned_data['date_modified']
date_bought = item_form.cleaned_data['date_bought']
shopping_list = item_form.cleaned_data['shopping_list']
quantity = item_form.cleaned_data['quantity']
deleted = item_form.cleaned_data['deleted']
return render_to_response ('base.html',{'user':user,'items':items,'item_form':item_form}, context_instance=RequestContext(request))
의 일부입니다.
아니하다. shopping_list에 쇼핑 목록 모델을 쿼리하여 초기 값을 가져오고 값을 찾을 수 없으면 오늘 날짜로 필드를 채 웁니다. –
문제는 'last_used_shoppinglist'를 양식 필드에 지정하는 것입니다. 원하지 않습니다. 보십시오 item_form.fields [ "shopping_list"]. 초기 = last_used_shoppinglist – Tom