网址.py
path('preferences/<action>/<category>/', views.preferences),
HTML表单
<form method="post">
{% csrf_token %}
{% for field in location_form %}
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<button type="submit" formaction="/wheelwatch/preferences/save/location/">Save</button>
<button type="submit" formaction="/wheelwatch/preferences/delete/location/">Delete</button>
</form>
视图.py
elif action == 'delete':
if category == 'location':
# match form data to CLLocation's location field
location_url = request.POST['location']
location_list = CLLocation._meta.get_field('location').choices
for location in location_list:
if location[0] == location_url:
CLLocation.objects.get(location=location[1], user__username=request.user).delete()
我收到“匹配查询不存在错误”。我知道数据库中有一个对象具有匹配的“用户”和“位置”字段,但我无法正确访问和删除它!你知道吗
型号.py
class CLLocation(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
location_choices = (
('https://auburn.craigslist.org/', 'AL - auburn'),
...
location = MultiSelectField(choices=location_choices)
我想问题是你删除的语法。你知道吗
您混合了两种不同的解决方案:获取实例,然后删除或直接删除。请尝试下列操作之一:
或者
见django help here
只需在删除查询中进行如下更改:
由于user本身是AuthUser的外键,所以只需要提供AuthUser对象作为参数。你知道吗
相关问题 更多 >
编程相关推荐