我正在开发一个网络应用程序,该应用程序在 IGDB 数据库中搜索用户选择的游戏,然后将它们显示在屏幕上。用户将能够单击添加按钮将所选游戏添加到他们作为用户的个人收藏中。现在我正在研究数据库模型,但我在构建集合模型时遇到了问题。我希望能够将每个游戏保存在用户特定的集合下。
这是我的models.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 %}
进行迁移时出现的错误是:
<块引用>AttributeError: 'function' 对象没有属性 'game_data'
我需要做什么才能将数据保存为游戏对象?我如何更广泛地将它作为一个集合来联系起来?
答案 0 :(得分:1)
如果是我,代码会是这样的:
# 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/