Поле 'id' ожидало число, но получило <категория: python >
Я пытаюсь отправить форму, но часть формы 'category' сохраняется. Я создаю приложение для заметок, в котором форма для заметок состоит из 3 элементов: Категория, Тема и Заметки. Категория должна быть уникальной, если она уже существует, то добавьте 'Тема' в существующую категорию, иначе создайте новую категорию, а затем добавьте в нее заметки соответственно.
Views.py
from django.shortcuts import render, redirect
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
# from django.contrib.auth.decorators import login_required
from .models import Category, Topic, Notes
# from .forms import RoomForm, UserForm
# from django.contrib.auth import authenticate, login, logout
# from django.contrib.auth.forms import UserCreationForm
# from django.contrib import messages
# Create your views here.
def home(request):
context = {}
return render(request, "base/home.html",context)
def make_notes(request):
categories = Category.objects.all()
topics = Topic.objects.all()
notes = Notes.objects.all()
if request.method == 'POST':
categories = Category.objects.get_or_create(
category = request.POST.get('category')
)
topics = Topic.objects.get_or_create(
category_name = categories,
name = request.POST.get('topic')
)
notes = Notes.objects.get_or_create(
topic_name = topics,
body = request.POST.get('body')
)
return redirect('notes')
context = {'categories':categories,'topics':topics,'notes':notes}
return render(request,'base/notes.html',context)
Models.py
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
class Category(models.Model):
category = models.CharField(unique=True, max_length=200)
def __str__(self) -> str:
return self.category
class Topic(models.Model):
category_name = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
# description = models.TextField(null=True, blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated','-created']
def __str__(self):
return self.name
class Notes(models.Model):
topic_name = models.ForeignKey(Topic, on_delete=models.CASCADE)
body = models.TextField()
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated','-created']
def __str__(self):
return self.body[0:20]
На странице мы нажимаем, сделать заметки и это перенаправляет нас на Notes.html.
Notes.html
{% extends 'base/main.html' %}
{% block content %}
<div class="main__div">
<div class="a">
<h2><strong>Notes</strong></h2>
<!-- will show sample notes if not logged in -->
<form action="" method="post">
{% csrf_token %}
<label for="">Enter a category</label>
<input type="text" name="category">
<label for="">Write your topic name...</label>
<input type="text" name="topic">
<label for="">Start noting here</label>
<input type="text" name="body">
<button type="submit">Submit</button>
</form>
<br><hr><br>
{% for note in notes %}
<h1>Category : {{note.topic_name.category_name}}</h1>
<p>Created {{note.created|timesince}} ago</p>
<h3>
{{note.topic_name}}
</h3>
<p>
{{note.body}}
</p>
<br>
{% endfor %}
</div>
<div class="b">
<h2><strong>Categories</strong></h2>
{% for category in categories %}
<p>{{category}}</p>
{% endfor %}
</div>
</div>
{% endblock %}
Домашняя страница:
{% extends "base/main.html" %}
{% block content %}
<div class="main__div">
<div class="a">
<h2><strong>
<a href="{% url 'notes' %}"> Make Notes</a>
</strong></h2>
<!-- will show sample notes if not logged in -->
</div>
<div class="b">
<h2><strong>Categories</strong></h2>
</div>
</div>
{% endblock content %}
Страница заметок: Изображение страницы примечаний
Ошибка :
Пожалуйста, помогите мне, что я упускаю? Также, пожалуйста, подскажите мне источник изучения Django, после которого я не буду делать хотя бы таких ошибок. Заранее спасибо.
Вот: Изменения, внесенные в views.py
def make_notes(request):
categories = Category.objects.all()
topics = Topic.objects.all()
notes = Notes.objects.all()
if request.method == 'POST':
categories = Category.objects.get_or_create(
category = request.POST.get('category')
)
topics, created = Topic.objects.get_or_create(
category_name = categories,
name = request.POST.get('topic')
)
notes, created = Notes.objects.get_or_create(
topic_name = topics,
body = request.POST.get('body')
)
return redirect('notes')
context = {'categories':categories,'topics':topics,'notes':notes}
return render(request,'base/notes.html',context)
Измените код представления make_notes следующим образом:
def make_notes(request):
categories = Category.objects.all()
topics = Topic.objects.all()
notes = Notes.objects.all()
if request.method == 'POST':
categories, created = Category.objects.get_or_create(
category = request.POST.get('category')
)
topics, created = Topic.objects.get_or_create(
category_name_id = categories.id,
name = request.POST.get('topic')
)
notes, created = Notes.objects.get_or_create(
topic_name_id = topics.id,
body = request.POST.get('body')
)
return redirect('notes')
context = {'categories':categories,'topics':topics,'notes':notes}
return render(request,'base/notes.html',context)
Вы можете следовать этой документации: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#get-or-create
Полагаю, проблема здесь:
categories = Category.objects.get_or_create(
category = request.POST.get('category')
)
Поскольку вы используете get_or_create
, вы получаете кортеж в переменной categories
. и затем пытаетесь установить этот кортеж как category_name
при создании Topic
, что приводит к ошибке.
См. документацию get_or_create.
Возвращает кортеж из (object, created), где object - извлеченный или созданный объект, а created - булево значение, указывающее, был ли создан новый объект. был ли создан новый объект.
Так что просто измените его на:
categories, created = Category.objects.get_or_create(
category = request.POST.get('category')
)