Please help me in views.py

32 views
Skip to first unread message

Amit Samanta

unread,
Sep 4, 2019, 12:07:49 AM9/4/19
to Django users
Hi,

I need help i am not understanding the keywords of joining and fetch value in veiw.py
i have three tables in model.py

Table 1(Author) :

Table 2(books):

Table 3 (details):
FK of table1
FK of table2

Fk = foreign key

I have to just show 

Author (author name)     Books (dynamic)     Books(author 2nd book)           ...
                                        details(dynamic)     details(dtl of author 2nd book) ...

I have created the template

now in views.py i an confused

book=books.object(i do not get what to do)
Author= (same )
details = (same)

Please can anybody help that will be very greatfull.
Thank you in advance



Thanks and Regards
Amit Samanta

Bhoopesh sisoudiya

unread,
Sep 4, 2019, 12:56:05 AM9/4/19
to django...@googlegroups.com
Hi Amit,

You can use 
=================≠=====================
Book.objects.select_related(FK).select_related(FK). values (select column name which you want).all()


Thanks 
Bhoopesh sisoudiya

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54483cc3-eadd-4559-8c61-c179cfbab933%40googlegroups.com.

Anirudh Jain

unread,
Sep 4, 2019, 2:39:49 AM9/4/19
to django...@googlegroups.com
Could you please elaborate/be more clear about what you are really trying to do ?

Amit Samanta

unread,
Sep 4, 2019, 3:58:24 AM9/4/19
to django...@googlegroups.com
I want try is...
there will be a dashboard

which will show

Author name

all the books which the author written

details of the book published date , avout the book in the above format


Amit Samanta

unread,
Sep 4, 2019, 3:59:29 AM9/4/19
to django...@googlegroups.com
now how can i get the name for author and details??

Lim Kai Wey

unread,
Sep 4, 2019, 4:04:57 AM9/4/19
to django...@googlegroups.com
Hey Amit,

I suggest you to follow this tutorial to build your project,
It teaches you all the basics that you need to know from scratch, which in my opinion will answer all the questions you're asking. 
The tutorial is about building a local library webpage which I believe is very similar to what you are doing now.

Hope this helps.

Regards,
Kai Wey

Amit Samanta

unread,
Sep 4, 2019, 5:10:03 AM9/4/19
to django...@googlegroups.com
i just need the veiw.py after this done i will try your tutorial

Anirudh Jain

unread,
Sep 4, 2019, 5:12:11 AM9/4/19
to django...@googlegroups.com
This can be done easily if you make a proper view function and render data from it in template properly.
A view function can be written like this :-

Here I am assuming that you want get the details of all the books/auuthors at one page. If you want details of a particular book, then the following function will be different.

from .models import Details, Author, Books
from django.shortcuts import render

view.py :-
def getdetails(request):
template_name = 'showDetails.html'
objs = Details.objects.all()
context = {'objs; : objs}
return render(request, template_name, context}


showDetails.html :-
(use this block in your html code wherever you want to show this data)

{% if objs %}
    
<table>
<tr>
<th>Author</th>
<th>Book</th>
</tr>

{% for obj in objs %}
<tr>
  <td>{{ obj.author_name }} </td>
  <td>{{ obj.book_name }} </td>
</tr>
{% endfor %}
</table>

{% endif %}

(In above html code, I have assumed that author_name and book_name are the FK in your Details table. Also, I am not sure




--
Anirudh Jain | Appreciate you taking time to read the mail.

Anirudh Jain

unread,
Sep 4, 2019, 5:12:59 AM9/4/19
to django...@googlegroups.com
Also, I am not sure how you have written html code, so use it properly according to your convinience.

Amit Samanta

unread,
Sep 4, 2019, 5:55:45 AM9/4/19
to django...@googlegroups.com
You have gussed correctly..
but i have taked book id as fk but i need the book name

Anirudh Jain

unread,
Sep 4, 2019, 6:41:07 AM9/4/19
to django...@googlegroups.com
Then simply use {{ obj.book_name.name }} in template where again, I am assuming 'name' is the column name you have give to book name in Books model

Amit Samanta

unread,
Sep 4, 2019, 8:11:12 AM9/4/19
to django...@googlegroups.com
by the above it is not giving any error but it is not showing the correspondence names and also it is showing all the fields in detail :-((

please help

Devdutt Bhati

unread,
Sep 4, 2019, 8:32:14 AM9/4/19
to django...@googlegroups.com
wiil get resolve the issue revert me.

On Wed, Sep 4, 2019 at 1:07 AM Devdutt Bhati <devdutt....@gmail.com> wrote:
if you created table in models.py. you can import table at view file like : from  appname .models import table/class name .
when you got the table data at views file then use it as variable=Tablename.objects.all() now get all values in variable. it can render in render function and use values at html page.
use for loop.

Devdutt Bhati

unread,
Sep 4, 2019, 8:32:14 AM9/4/19
to django...@googlegroups.com
def getdetails(request11):
     objs = Details.objects.all()  
     context = {'objs; : objs}  
    return render(request, 'full_path_of_template', context}  

templates in which folders path should set in setting file. try this all the best and revert me.
view.py :-
def getdetails(request):
template_name = 'showDetails.html'
objs = Details.objects.all()
context = {'objs; : objs}
return render(request, template_name, context}


I’m protected online with Avast Free Antivirus. Get it here — it’s free forever.

Devdutt Bhati

unread,
Sep 4, 2019, 8:32:14 AM9/4/19
to django...@googlegroups.com
if you created table in models.py. you can import table at view file like : from  appname .models import table/class name .
when you got the table data at views file then use it as variable=Tablename.objects.all() now get all values in variable. it can render in render function and use values at html page.
use for loop.

Amit Samanta

unread,
Sep 4, 2019, 8:38:52 AM9/4/19
to django...@googlegroups.com
i get all thank you for your help..
the actuall funda is 
i have to show author name with related to that i have to show books name of the same author now the detail related to the books
this is wht i need
:-(

Amit Samanta

unread,
Sep 4, 2019, 10:21:30 AM9/4/19
to django...@googlegroups.com
Its done now.. thank u everybody

Now i want to do that it will show only the latest book with detail of a author


Amit Samanta

unread,
Sep 5, 2019, 3:45:11 AM9/5/19
to django...@googlegroups.com
Hi everybody thank you for helping me throughout...

lastly do anyone know how to group by in django orm


 or if i give a db query can anyone help me in changing it to django orm??
please
Thank you in advance
Amit Samanta
Reply all
Reply to author
Forward
0 new messages