Reputation: 11
I am working on a web-app that searches the IGDB database for games of a user chosen title, then displays them on screen. Users will be able to click an add button to add the chosen game to their personal collection as a user. Right now I am working on the database models and I am having trouble with the collections model I am building. I want to be able to save each game under a user specific collection.
This is my 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
This is my saving function in 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')
And the 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 %}
The error I am being given when making migrations is:
AttributeError: 'function' object has no attribute 'game_data'
What do I need to do to save the data as a Game object? And how do I relate it back as a collection more generally?
Upvotes: 0
Views: 62
Reputation: 6331
If me, code will be sth like this:
# 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')
I don't know how to change your template. May be you can get answer from document: https://docs.djangoproject.com/en/3.2/ref/forms/
Or tutorial https://docs.djangoproject.com/en/3.2/intro/tutorial01/
Upvotes: 1