How to compare models using id's Django - python

Can anyone please help in tackling the following problem?
Suppose I have a model named Answer and a form for user input named CheckAnswer.
What I want is that when a particular Question page is openend and the user types the answer, it should get checked with the corresponding answer
The answer from the model can be accessed using the id. but how can i specify the id of question page that opens up and link it to the answer id.
Below are the codes attached
forms.py
from django import forms
from .models import Answer
from django.core.exceptions import ObjectDoesNotExist
class CheckAnswer(forms.Form):
your_answer=forms.CharField(label='Answer')
def clean(self):
cleaned_data=super(CheckAnswer,self).clean()
response=cleaned_data.get("your_answer")
try:
p = Answer.objects.get(id=1,answer__iexact=response)
except Answer.DoesNotExist:
raise forms.ValidationError("Wrong Answer.")
views.py
from django.shortcuts import render,redirect
from django.views.generic import *
from . import models
from django import forms
from .forms import CheckAnswer
from django.contrib.auth.decorators import login_required
# Create your views here.
#login_required
def Arena1(request):
if request.method=='POST':
form = CheckAnswer(request.POST)
if form.is_valid():
return redirect('thanks')
else:
form=CheckAnswer()
return render(request,'levels/arena1.html',{'form':form})
models.py
from django.db import models
from django.contrib.auth import get_user_model
User=get_user_model()
users=User.objects.all()
# Create your models here.
class Answer(models.Model):
name=models.CharField(max_length=10,unique=True)
answer=models.CharField(max_length=100)
def __str__(self):
return self.name
class Meta:
ordering= ["-name"]

Related

How to match Questions with Answers in django

Please explain me how to match the Answer of a particular Question in django.
Right Now I am facing the issue that whatever the user inputs in the form if it exits in Answer database it returns True, But what I want is that whatever the user inputs should be matched with the Answer of that particular Question and not any of the Answers present.
models.py
from django.db import models
from django.contrib.auth import get_user_model
from PIL import Image
User=get_user_model()
# Create your models here.
class Question(models.Model):
question_name=models.CharField(max_length=20,unique=True,default=' ')
question_image=models.ImageField(upload_to='levels/')
question_text=models.TextField(blank=True,max_length=100)
def __str__(self):
return str(self.question_name)
class Meta:
ordering= ["question_name"]
class Answer(models.Model):
question_relation=models.ForeignKey(Question,on_delete=models.CASCADE)
answer=models.CharField(max_length=100,unique=True)
def __str__(self):
return self.answer
forms.py
from django import forms
from .models import Answer,Question
from django.core.exceptions import ObjectDoesNotExist
from . import views
class CheckAnswer(forms.Form):
your_answer=forms.CharField(label='Answer')
def clean(self):
cleaned_data=super(CheckAnswer,self).clean()
response=cleaned_data.get("your_answer")
try:
p = Answer.objects.get(answer__iexact=response)
except Answer.DoesNotExist:
raise forms.ValidationError("Wrong Answer.")
views.py
from django.shortcuts import render,redirect,get_object_or_404
from django.views.generic import *
from . import models
from django import forms
from .forms import CheckAnswer
from django.contrib.auth.decorators import login_required
from .models import Question
from dashboard.models import UserLoggedIn
# Create your views here.
#login_required
def Arena1(request):
if request.method=='POST':
form = CheckAnswer(request.POST)
if form.is_valid():
return redirect('thanks')
else:
form=CheckAnswer()
args={'form':form}
return render(request,'levels/arena1.html',args)
I think the concept of pk will be used but I dont know how to implement it
Please update my code that solves this problem.
First of All, Matching the Question would Require you to Store of Provide the Question to the Function Somehow. What You can do it use a Global Variable or a Container in which you would set the value while rendering the Question and then Import it to forms.py and set the your_question = Question
forms.py
from django import forms
from .models import Answer,Question
from django.core.exceptions import ObjectDoesNotExist
from . import views
class CheckAnswer(forms.Form):
your_answer=forms.CharField(label='Answer')
def clean(self):
cleaned_data=super(CheckAnswer,self).clean()
response=cleaned_data.get("your_answer")
try:
p = Answer.objects.get(answer__iexact=response)
if(your_answer == p.answer and your_question == p.question_relation.question_text): #user inputs should be matched with the Answer of that particular Question
#Code To Run for Correct Answer Goes Here
else:
#The Answer is Wrong or Does not Match the Particular Question
raise forms.ValidationError("Wrong Answer.")
except Answer.DoesNotExist:
raise forms.ValidationError("Wrong Answer.")

Django view not saving to model

I have a view I am calling when a user clicks a button on the site, it pulls JSON data(from the Coinbase API) and converts it to a string and should save the string pulled to the current user's account.
Whenever they click the button it will pull the string, but nothing is saved to the account, which is the issue.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from ico_login.models import UserAddress
from coinbase.wallet.client import Client
#login_required()
def generate_address_btc(request, *args, **kwargs):
client = Client('api', 'key')
r = client.get_addresses('account_id')
address = r['data'][0]['address']
request.user.address = str(address)
request.user.save()
return HttpResponse(address)
models.py
from django.db import models
from django.contrib.auth.models import User
class UserAddress(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.CharField(max_length=300, default=' ')
urls.py
from django.contrib import admin
from django.urls import path, include
from ico_details import views
urlpatterns = [
path('create_address/', views.generate_address_btc, name='generate')
]
In your view you write request.user.address = ..., so you add the attribute address to a User object. As far as I read your code, this is not what you want. You would like to put str(address) into the field address of the object UserAddress related to the User object from request.user, right?!
So, here are the suggested edits to do so:
models.py
from django.db import models
from django.contrib.auth.models import User
class UserAddress(models.Model):
# next line changed
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_address')
address = models.CharField(max_length=300, default=' ')
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from ico_login.models import UserAddress
from coinbase.wallet.client import Client
#login_required()
def generate_address_btc(request, *args, **kwargs):
client = Client('api', 'key')
r = client.get_addresses('account_id')
address = r['data'][0]['address']
# next 2 lines changed
request.user.user_address.address = str(address)
request.user.user_address.save()
return HttpResponse(address)
Hope that helped and happy coding!

Django IntegrityError-NOT NULL constraint failed: portfolio_myportfolio.holder_id

I am trying to make a portfolio manager app in django. I tried making the holder as a primary key. But in that case, the new entry overlaps the old one. So, I deleted that database and started afresh. Now, when I am trying to add a portfolio with a logged in user, I am getting the error as under:-
IntegrityError-NOT NULL constraint failed: portfolio_myportfolio.holder_id
I also wanted to set my database in such a way that If user adds the quantity of the same stock with same nsecode, it should add up in the previous entry instead of making a new row. In that case, what should be my scenario.
Since, I am very new to python and django, my code is not clean and might have few lines of code, which is un-necessary, any help in removing such errors would be much appreciated.
My Model is
from django.db import models
from django.contrib.auth.models import User
codes=(tuple of tuples)
class MyPortfolio(models.Model):
nsecodes = models.CharField(max_length=10, choices=codes)
quantity = models.PositiveIntegerField()
buyvalue=models.DecimalField(max_digits=15,decimal_places=2)
holder = models.ForeignKey(User, related_name='myportfolio')
# Create your models here.
my views. py is :
from django.shortcuts import render,redirect,get_object_or_404
from django.views.generic.edit import CreateView
from .models import MyPortfolio
from .forms import AddNewForm
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
def home(request):
return render(request,'home.html')
#login_required
def my_portfolio(request):
if request.user.is_authenticated():
myportfolios=MyPortfolio.objects.filter(holder=request.user)
return render(request, 'my_portfolio.html',{'myportfolios':myportfolios})
#login_required
def add_new_form(request):
if request.user.is_authenticated():
myportfolios = MyPortfolio.objects.filter(holder=request.user)
if request.method=="POST":
form=AddNewForm(request.POST)
if form.is_valid():
myportfolio=form.save()
myportfolio.nsecodes=form.cleaned.data.get('nsecodes')
myportfolio.buyvalue = form.cleaned.data.get('buyvalue')
myportfolio.quantity = form.cleaned.data.get('quantity')
myportfolio.holder=request.user
myportfolio.save()
return redirect('my_portfolio')
else:
form=AddNewForm()
return render(request,'add_new.html',{'form':form,'myportfolios':myportfolios})
my forms.py is as under:-
class AddNewForm(forms.ModelForm):
nsecodes=forms.ChoiceField(choices=codes, required=True)
quantity=forms.IntegerField(min_value=1,required=True)
buyvalue=forms.DecimalField(max_digits=10, decimal_places=2,required=True,min_value=.01)
class Meta:
model=MyPortfolio
fields=['nsecodes','quantity','buyvalue']
you are saving the data before assigning the foreign key id
try this
if form.is_valid():
myportfolio=form.save(commit=False)
myportfolio.holder=request.user
myportfolio.save()
return redirect('my_portfolio')

[Django]How to get the str(id) of a ModelA object in order to give the source code of a page as initial value of a CharField that belongs to a ModelB?

I started to code a two-apps django project. ModelA belongs to appone and ModelB belongs to apptwo. My purpose is to create a ModelA instance everytime that the user creates a ModelB instance. And the value of a ModelA CharField (that is ckeditor widgeted) must be the source code of a ModelB admin view. I used a post_data signal to link a function of creation for that. The problem is that i use the id of each instance of ModelB in order to create the good content for each instance of ModelA. When I try to use a string of the url sending the id parameter, the content field has for value the source code of the debug page
(error 500, DoesNotExist at /admin/apptwo/modelb/my_view/ref=76, [76 is an example] ModelB matching query does not exist. Exception location : /home/me/Desktop/env/lib/python3.5/site-packages/django/db/models/query.py in get, line 385)
But when I try to visit the url "http://localhost:8000//admin/apptwo/modelb/my_view/ref=76", or when I hardcode the url, without a str(instance.id), the page exists and everything works perfectly.
I don't understand why.
Could anybody give me some help to solve this problem ?
Thanks in advance,
PS :
The first app has a model.py that contains the following code :
from django.db import models
from django.contrib.auth.models import User
from registre.models import *
class ModelA(models.Model):
content = models.CharField(max_length=255, null=True)
def __str__(self):
return "ModelA : " + str(self.id)
the admin.py of this first app also contains :
from django.contrib import admin
from appone.models import *
from apptwo.models import ModelB
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.conf.urls import url
from registre import views
from django.db.models.signals import post_save
from django.dispatch import receiver
import datetime
from django.contrib.auth.models import User
from django import forms
from ckeditor.widgets import CKEditorWidget
from django.template.loader import render_to_string
import requests
class ModelAAdminForm(forms.ModelForm):
content = forms.CharField(widget=CKEditorWidget())
class Meta:
model = ModelA
fields = '__all__'
class ModelAAdmin(admin.ModelAdmin):
form = ModelAAdminForm
def create_A(sender, instance, **kwargs):
string = "http://localhost:8000/admin/apptwo/modelb/my_view/ref=" + str(instance.id)
r = requests.get(string)
ModelA.objects.create(contenu=r.text.encode('utf-8'))
post_save.connect(create_A, sender=ModelB)
admin.site.register(ModelA, ModelAAdmin)
the second app (apptwo) has a models.py like this :
from django.db import models
from django.contrib.auth.models import User
class ModelB(models.Model):
owner = models.ForeignKey(User, null=True)
name = models.CharField(max_length=255, null=True)
def __str__(self):
return self.name
and an admin.py that contains :
from django.contrib import admin
from appone.models import *
from apptwo.models import *
import datetime
from django.conf.urls import url, include
from django.template.response import TemplateResponse
class ModelBAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(ModelB, self).get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(owner=request.user)
def save_model(self, request, obj, form, change):
obj.owner = request.user
obj.save()
def get_urls(self):
urls = super(ModelBAdmin, self).get_urls()
my_urls = [
url(r'^my_view/ref=(?P<id>\d+)$', self.my_view),
]
return my_urls + urls
def my_view(self, request, id):
context = dict(
self.admin_site.each_context(request),
selector = ModelB.objects.get(id=id),
)
return TemplateResponse(request, "myview.html", context)
admin.site.register(ModelB, ModelBAdmin)
and finally a template myview.html with :
<p>Test {{ selector.name }}</p>

Django ModelForm validate if input exists in model

I have a model Linea with only one field named numero, wich is a charset (the values of numero are supposed to be numbers) . I want to do a form where the user can search for a value of numero. If the number that the user inputs exists in the database the view will redirect to a template that shows information about that value of numero, but if the value that the user search is not in the database it will throw an error.
I'm very new in Django, and I've searched the way to do it but I can't achieve it.
forms.py
from django import forms
from .models import Linea
class LineaForm(forms.ModelForm):
class Meta:
model = Linea
fields = ('numero',)
models.py
from __future__ import unicode_literals
from django.db import models
class Linea(models.Model):
numero = models.CharField(max_length=2)
def __str__(self):
return self.numero
views.py
from django.shortcuts import render, redirect, reverse
from .forms import LineaForm
from django.http import HttpResponseRedirect
import googlemaps
from datetime import datetime
def lineas(request, template="bustopaplineas.html"):
if request.method == "POST":
form = LineaForm(request.POST)
if form.is_valid():
numero = form.save(commit=False)
linea_numero = str(numero)
return redirect('lineas_detalles', linea_numero=linea_numero)
else:
form = LineaForm()
return render(request, 'bustopapp/lineas.html', {'form': form})
Thanks in advance.
You can check in the Linea table if it exists or not:
Linea.objects.get(numero=input)
In case, it doesn't it will throw an exception

Categories