Django Attribute试图通过model.py将对象保存到数据库时出错



我正在开发一个web应用程序,该应用程序在IGDB数据库中搜索用户选择标题的游戏,然后将其显示在屏幕上。用户可以点击添加按钮,将所选游戏作为用户添加到他们的个人收藏中。现在我正在研究数据库模型,我在构建集合模型时遇到了问题。我希望能够将每个游戏保存在用户特定的收藏中。

这是我的型号.py:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Game(models.Model):
collector = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50, blank=True, null=True)
cover = models.CharField(max_length=50, blank=True, null=True)
storyline = models.TextField(max_length=500, blank=True, null=True)
summary = models.TextField(max_length=500, blank=True, null=True)
genre_set = models.CharField(max_length=50, blank=True, null=True)
def __str__(self):
return self.name

class Collection(models.Model):
from .views import save
collector = models.ForeignKey(User, on_delete=models.CASCADE)
colletion = models.ManyToOneRel(save.game_data, null=True, blank=True)

def __str__(self):
return self.name 
pass

class Counter(models.Model):
def increment(self):
self.counter += 1
def get_value(self):
return self.counter

这是我在views.py中的保存函数:

from igdb.wrapper import IGDBWrapper
from django.views.generic import ListView
from catolog.models import Game
from django.shortcuts import render
import json
import requests
exp_time = False
# Create your views here.
def save(request, message_json):
from catolog.models import Counter
# Must point to obj index from results on page
game_data = message_json[Counter.get_value]
game_data.save()
return render(request, 'catolog/search_page.html')

和html:

{% extends "home/base.html" %}
{% load static %}
{% block content %}
<link rel="stylesheet" type="text/css" href="{% static '/css/button.css' %}">
<h2>Search for a game!</h2>
<div class="search-bar" id="searchDiv">
<form action="{% url 'search' %}" method="POST" id="searchForm" style="left: 0;
position: absolute;
text-align: center;
width: 100%;
height: 20px;
z-index: 100;" >
{% csrf_token %}
<input type="text" formaction="{% url 'search' %}" name="user_input" id="id_q" placeholder="Search..." >
<!-- <button formaction="{% url 'search' %}">Sumbit</button> -->
</form>
</div>
<div>
<br>
<br>
{% for key, game in group_games.items %}
<fieldset class="card" style="width: 40%; margin: auto; background-color: rgba(255, 187, 0, 0.692); color: black; border-radius: 25px; padding: 10px">
<ul>
{% if game.cover_url %}
<img src="{{ game.cover_url }}" alt="Cover Art" style="width: 200px; height: 280px;">
<br>
{% endif %}
<ul style="text-align: center; font-weight: bold;">{{ game.name }}</ul>
<br> 
{% block content %}
{% endblock content %}
<br>
<fieldset style="border-color: black;">
<legend style="color: black; font-weight: bolder;">Genres:</legend>
<ul style="font-weight: bolder; align-items: center;">{{ game.genres }}</ul>
</fieldset>
</ul>
<form action="{% url 'saveGame' %}" method="POST">
{ counter.increment }
<button class="name noselect" type="submit" id="wishlistBtn">Wishlist It</button>
</form>
</fieldset>
{% endfor %}>
</div>
{% endblock content %}

我在进行迁移时遇到的错误是:

属性错误:"函数"对象没有属性"game_data"

我需要做什么才能将数据保存为游戏对象?我该如何将其作为一个集合进行更广泛的关联?

如果是我,代码将是这样的:

# models.py
from django.db import models
from django.contrib.auth.models import User
class Game(models.Model):
name = models.CharField(max_length=50)  # name should not be null
cover = models.CharField(max_length=50, blank=True, null=True)
storyline = models.TextField(max_length=500, blank=True, null=True)
summary = models.TextField(max_length=500, blank=True, null=True)
genre_set = models.CharField(max_length=50, blank=True, null=True)
def __str__(self):
return self.name

class Collection(models.Model):
collector = models.ForeignKey(User, on_delete=models.CASCADE)
games = models.ManyToManyField(Game, blank=True)

def __str__(self):
return self.name 

class Counter:
counter = 0
@classmethod
def increment(cls):
cls.counter += 1
@classmethod
def get_value(cl):
return cls.counter

views.py

def save_collection(request, game_id):
Counter.increment()
# Must point to obj index from results on page
game = Game.objects.get(id=game_id)
collection, _ = Collection.objects.get_or_create(collector=request.user)
collection.games.add(game) 
return render(request, 'catolog/search_page.html')

我不知道如何更改你的模板。也许你可以从文件中得到答案:https://docs.djangoproject.com/en/3.2/ref/forms/

或教程https://docs.djangoproject.com/en/3.2/intro/tutorial01/

最新更新