last day (15 days later) » 

4:29 PM
I need django help,

I have created 2 model class 1 is Product another one Result.
I have used below class at views.py but below class only treager Product model not Result model,
my question is how can I use multiple model at one views class? I am learning, please help me, someone.

class user_checkout(ListView):
    template_name = 'user_checkout.html'
    context_object_name = "product_list"
    def get_queryset(self):
        return Product.objects.all()
 
4:47 PM
I'm not a django user but surely you need to show the view function here?
@KokHyvv please see the formatting guide for chat
 
@roganjosh thanks for this point
 
@KokHyvv I'm going to move your post in the meantime. Please do repost with the correct formatting, though
 
5:08 PM
@NIKHILCHANDRAROY
just add these lines to views.py page
from itertools import chain
from operator import attrgetter
then add these to use multiple models in views

def user_checkout(request):
first_model = yourmodelname.objects.all()
scnd_model = yourmodelname.objects.all()

results = list(
sorted(
chain(first_model , scnd_model),
key=lambda x: x.id,reverse=True))

context = {'results':results}
return render(request,'html_diroctry/user_checkout.html',context=context)
 
@KokHyvv How does that relate to the django ORM?
 
@roganjosh is that good ? and okay with rules ?
 
Oh come on, I already linked how to format code
 
@roganjosh it does
 
@KokHyvv you tell me; does that look like valid python?
 
5:12 PM
No, it's Django, is it forbidden to post anything about it?
 
No, I understand perfectly well what django is and it's not against the rules to post about it. this has completely broken indentation, just like the last posts I moved. this makes absolutely no sense in relation to the problem
 
oh okay that's good ,, but this is the answer to @NIKHILCHANDRAROY question
it will work perfect
this is the correct code
https://filebin.net/xcioskoibthm2miw/views.txt?t=626jre2k
 
No, it won't. I'm not sure how much clearer I need to be. It's possible that the text you've written that you think is valid python code might do something, but the indentation is broken so nobody can actually test it
 
i am using this code in my project and it's work
you can check it if you want
so what is the wrong in my code ?
 
You have a deep misunderstanding here
from itertools import chain
from operator import attrgetter
You don't use either of those methods
Those imports do nothing in your code
 
5:22 PM
" chain " need to import
from itertools import chain
if you don't import this line it will show error
 
I tell a lie, the first import does do something in your code, but not in the problem we're trying to solve
from operator import attrgetter doesn't, though
 
Maybe I got it wrong, but the first one will need it
please remove this line and try , and if you get error add this line
from operator import attrgetter
 
Sure, you need that to make your code work. Now please explain how that import fixes the problem that was asked by NIKHIL
 
okay , first this line
from itertools import chain
is allow you to use multiple models in single view
and when you use the way that i show it to you ,, you can use multiple models in single view and single template
also this can help you when you want to use search from multiple models
 
@KokHyvv How? What would be the actionable step I could take?
 
5:30 PM
can you provide your code please to edit code with correct way
give me full views.py code and name of template directory that have user_checkout.html
 
I know I'm making your life hard @KokHyvv but all we've got is this and your posts have broken indentation, so I'm inclined to think it's way under-specified and we're going down a hole
 
just give me full code and i will edit it and give you the correct code
 
Oh man, it's not my code :/
"What would be the actionable step I could take?" is a hypothetical to try make you explain your steps
 
yes i know that , i mean @NIKHILCHANDRAROY
i did , in the previous answer
1- add this line
from itertools import chain
2- add https://filebin.net/xcioskoibthm2miw/views.txt?t=inx7fqjw
and it will work perfect
* note : you must edit " website_primary_html_pages " to yours where you store html page
 
Ok, that's enough. You seem to think that importing libraries will make things work without seeing any kind of error at all. It has nothing to do with the django orm
 
5:37 PM
@KokHyvv understand, but how can I use this one at template html, context variable not giving me the result
{% for c in context %}
{{c.salary_name}}


{% endfor %}
I need there salary_name, then how?
 
check this code to use them in html page
https://filebin.net/99mqsemm96167wac/html.txt?t=dfmlqo2i
@roganjosh you will see
it will work with @NIKHILCHANDRAROY
 
@NIKHILCHANDRAROY if you look into the code carefully, it does not define context but results.
@KokHyvv also, for you, there is no need to do itertools chan when you'll have a list
 
by this way you can use multiple model in one single view and template html page

But if you want to use multiple models and control each model separately, tell me
 
results = sorted([*first_list, *second_list], key=attrgetter('id'), reverse=True)
also @KokHyvv please use a proper paste bin instead of that annoying filebin.
 
@AnttiHaapala i am using multiple model by this way and it's work
okay i will try use other one
 
5:48 PM
@KokHyvv yes chain works, but it is annoying and ugly in this case. It is meant for chaining iterables that are never materialized in one go. Sorted will materialize a list anyway, so might do it explicitly with less characters. We Pythonistas strive for enlightenment - embrace the Zen of Python.
 
all of code here

https://codeshare.io/2jylev
@AnttiHaapala There may be other methods, but this method is good
if you want to use multiple models and control each model separately, tell me
and i will help you with same method
 
In any case @KokHyvv your code doesn't answer the problem of ListView
 
@AnttiHaapala yeah, the ListView won't resolve because I don't know that method, so I gave it another one
 
@KokHyvv thanks for your helpful support, I am new learning django. I have a lot of question to ask but some are wasting time to find the right solution.
 
Yes, I suffer from this problem too, Haha , does my code work with you ?
and u are welcome , ask what you want
 
6:03 PM
yes, it's working but I am getting some more issue of it
 
okay tell me what is the issue
 
Imagine, In first_model if I have total variable and same variable has second_model how can I get it at template?
and when I create some value at first_model after that when I create second_model value then second_model get some extra empty tag
need those model separate, they have no relation each other but need to use same page like one view function
 
What models?
 
and 1 more, if I use this model IntegerField and how can I get total value

ict = models.IntegerField()
economics = models.IntegerField()
total = models.IntegerField() # need to sum the value ict+economics, how can it possible?
 
when you use this method it already show data from two models in html page
wait
 
6:15 PM
ok
 
can you give me your model code and tell me what data you want to show from it
 
This needs to be pretty spectacular because I don't think any of this discussion has been useful for people and I'm really tempted to move it to another room for you guys. I don't think it's good practice at all
 
# This are the 2 model that I need to show at index.html template separate table
# and ict+economics value should go automaticaly at total variable value.
class Result(models.Model):
    student_name = models.CharField(max_length=50)
    bangla = models.IntegerField()
    english = models.FloatField()
    ict = models.IntegerField()
    economics = models.IntegerField()
    total = models.IntegerField()
    description = models.CharField(max_length=50)
    def __str__(self):
        return self.student_name
 
Things like this. Well, maybe you're right, but I won't see it, will I? I've not seen a compelling argument from you that explains what you're doing
 
okay i am understand you what do you want to grab ?
student_name or what
@NIKHILCHANDRAROY?
 
6:24 PM
prntscr.com/uoesmm see this screenshot
in template
{% for r in result %}
<tr>
<td> {{r.student_name}} </td>
<td> {{r.bangla}} </td>
<td> {{r.english}} </td>
<td> {{r.ict}} </td>
<td> {{r.economics}} </td>
<td> {{r.total}} </td>
<td> {{r.description}} </td>
</tr>
{% endfor %}
 
okay can you tell me this point
if you have control with these two models
if first model you want to do this in html right ?

{% for r in first_model %}
<tr>
<td> {{r.student_name}} </td>
<td> {{r.bangla}} </td>
<td> {{r.english}} </td>
<td> {{r.ict}} </td>
<td> {{r.economics}} </td>
<td> {{r.total}} </td>
<td> {{r.description}} </td>
</tr>
{% endfor %}

right ?
and what do you want to grab from 2nd model ?
okay wait me
 
above code is second model and when I create value for first_model the second one get empty html tag due to for loop
see screenshot please,
 
okay bro it's simple just see new edited code please and it will solve your issues
https://codeshare.io/2jylev
@NIKHILCHANDRAROY do it and tell me
 
'list' object has no attribute 'first_model'
I call this way {% for r in result %}
 
please see this new code here and use it bro
https://codeshare.io/2jylev
hello let's talk here
 
6:39 PM
94 messages moved from Python
 
.
 
ok, it's finally working
but, how can I get the total value automatically from IntegerField
ict = models.IntegerField()
    economics = models.IntegerField()
    total = models.IntegerField()
total = models.IntegerField() # this one should calculate IntegerField automatically not manullay typing
ict+economics like this,
 
wait , can you explain now what do you need for ?
i mean you want get the total value automatically from where ?
 
from this variable
economics = models.IntegerField()
total = models.IntegerField()
need to generate the value at total variable
# total = models.IntegerField(economics + total ) like this but I don't understand how can it possible
 
6:56 PM
okay wait
 
7:12 PM
please check these

1- https://stackoverflow.com/questions/57755460/calculating-two-integers-fields-in-django

2- https://community.simpleisbetterthancomplex.com/t/how-to-calculate-in-django-model-fields/601
@NIKHILCHANDRAROY
 

last day (15 days later) »