Я пытаюсь получить доступ к своему объекту, но созданное мной поле сообщает мне, что его нет.
$ cat userdash / views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import AssetList, Items
from .forms import CreateNewTrade
# Create your views here.
#def index(response):
# return HttpResponse("<h1>Hello Dark World!</h1>")
def userdash(response, id):
ls = AssetList.objects.get(id=id)
if response.method == "POST":
print(response.POST)
if response.POST.get("save"):
for item in ls.items_set.all():
if response.POST.get("c" * str(item.id)) == "clicked":
item.sellbtn = True
else:
item.sellbtn = False
item.save()
elif response.POST.get("newItem"):
txt = response.POST.get("new")
if len(txt) > 2: #this validation is retarded and needs to be fixed
ls.items_set.create(user_asset=txt, sellbtn=False) #this is the line in question
else:
print("invalid")
#items = ls.items_set.get(id=1)
#return HttpResponse("<h1>User Dashboard!</h1><h2>%s</h2><br></br><p>%s</p>" %(ls.name, str(items.user_asset)))
return render(response, "userdash/list.html", {"ls":ls})
def home(response):
#pass
return render(response, "userdash/home.html", {})
def create(response):
if response.method == "POST":
form = CreateNewTrade(response.POST)
if form.is_valid():
n = form.cleaned_data["user_asset"]
t = AssetList(name=n)
t.save()
return HttpResponseRedirect("/userdash/%i" %t.id)
else:
form = CreateNewTrade()
return render(response, "userdash/create.html", {"form":form})
$ cat userdash /templates/userdash/list.html
{% extends 'userdash/base.html' %}
{% block title %}
List Page
{% endblock %}
{% block content %}
<h2>{{ls.name}}</h2>
<form method="post" action="#">
{% csrf_token %}
<ul>
{% for item in ls.items_set.all %}
{% if item.sell_asset == True %}
<li><input type="checkbox", value="clicked", name="c{{item.id}}" checked>{{item.user_asset}} - SOLD</li>
{% else %}
<li><input type="checkbox", value="clicked", name="c{{item.id}}">{{item.user_asset}} - NOT SOLD</li>
{% endif %}
{% endfor %}
</ul>
<button type="submit", name="save", value="save">Save</button>
<input type="text", name="new">
<button type="submit", name="newItem", value="newItem">Add Item</button>
</form>
{% endblock %}
$ cat userdash / forms.py
from django import forms
class CreateNewTrade(forms.Form):
#form fields are same as models and database
user_asset = forms.CharField(label="User Asset", max_length=200)
sellbtn = forms.BooleanField(required=False)
$ cat userdash / models.py
from django.db import models
# Create your models here.
class AssetList(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Items(models.Model):
currencylist = models.ForeignKey(AssetList, on_delete=models.CASCADE)
user_asset = models.CharField(max_length=300)
sell_asset = models.BooleanField()
def __str__(self):
return self.user_asset
Ошибка:
TypeError at /userdash/3
Items() got an unexpected keyword argument 'sellbtn'
Request Method: POST
Request URL: http://192.168.42.14:8081/userdash/3
Django Version: 3.0.5
Exception Type: TypeError
Exception Value:
Items() got an unexpected keyword argument 'sellbtn'
Exception Location: /home/piggy/Env/lib/python3.7/site-packages/django/db/models/base.py in __init__, line 500
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.7.3
Python Path:
['.',
'',
'/home/piggy/Env/lib/python37.zip',
'/home/piggy/Env/lib/python3.7',
'/home/piggy/Env/lib/python3.7/lib-dynload',
'/usr/lib/python3.7',
'/home/piggy/Env/lib/python3.7/site-packages']
Server time: Mon, 4 May 2020 20:46:11 +0000
Как мне получить доступ к полю sellbtn в моем объекте?
Бонусный вопрос:
Какой самый простой способ отобразить содержимое объекта при такой отладке ?