Bullet
3 times.. 3 prefetchs
Ali Hasan
It didn't have the .distinct() originally but saw online and thought it might fix the issue (it didn't)
Ali Hasan
Ok what I need is get that department object with its related things (it has 3 branches)
Ali Hasan
not the same object 3 times (each time it has all its 3 branches)
Bullet
mmm..
Bullet
Try with .. select_related()
Ali Hasan
Try with .. select_related()
It doesn't get the objects of reversed relations
Ali Hasan
I've tried using the Prefetch() and it doesn't solve the problem either
Bullet
Maybe, maybe... that´s could be the point.. in the reverse.. .prefetch_related override distinct() and prefetch_related show each data in 'branches', 'branches__programs', 'branches__programs__stages' as a different object
Bullet
try using just one branch
Ali Hasan
A single department has many branches , each branch has many programs , each program has many stages , I'm trying to get that department , with all its branches , and each of its branches with all their programs , and each program with all its stages , so basically: ```{ branch1: { program1: { stage1: stage2: stage3: } program2: { //stages } branch2: { //same goes here } }```
Bullet
try to using .objects.prefetch_related('branches').all() then distinct and then orderby name ...the way u build the query is the reason..
Bullet
I´m just saying..
Д
Is there a way to solve this ?
Simplify the problem/query until it's too obvious.
Д
I did over here
But write the code then line by line. Step by step.
Д
Comment out what you have and rewrite it
Ali Hasan
But write the code then line by line. Step by step.
what do you mean ? Sorry but I'm confused a bit 😅 If you mean to write my query code , then I did send it above
Д
Remember: keep the code *too* easy
Maxim
Is there a way to solve this ?
you can get a list not of the objects themselves, but only pk, then ensure their uniqueness, and then do a selection on the set of pk
Ali Hasan
Remember: keep the code *too* easy
departments = list(Department.objects.filter( id__exact=some_ID ).prefetch_related('branches', 'branches__programs', 'branches__programs__stages'))
Ali Hasan
Is this what you mean ?
Ali Hasan
you can get a list not of the objects themselves, but only pk, then ensure their uniqueness, and then do a selection on the set of pk
The department ID is unique for sure , each branch has unique ID , each Program has a unique ID, each stage has a unique ID I'm getting exactly what I want , only 3 copies instead of one (duplicated). and 3 is the number of branches that this department has.
Ali Hasan
maybe the order_by is making the issue ?
Ali Hasan
Yes , the order_by is making the duplicates issue
Ali Hasan
I found the issue , when I remove 'branches__name' in the order_by( ) it gets one copy , but when I add it, it brings duplicates. I think using the Prefetch object and ordering the branches alone here will solve the issue.
Ali Hasan
Thanks a lot to you guys 🙏🏻
Tohid
Hey guys Please introduce a relatively advanced training project for Django Thanks 😊
Doragonsureiyā
Hey guys Please introduce a relatively advanced training project for Django Thanks 😊
Looking for Django tutorials? you can follow these three recommended ones: * Official documentation and tutorial * Tutorial from MDN * Tutorial from django-girls
Shazeb
why i'm getting this many messages?
Shazeb
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Quick High Protein</title> {% block extra_head %} {% endblock extra_head %} </head> {% tailwind_css %} <body class="bg-gray-50 leading-normal tracking-normal font-body"> {% if messages %} <ul class="messages"> {% for message in messages %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> {% endfor %} </ul> {% endif %} {% block content %} {% endblock content %}
Shazeb
https://prnt.sc/fIi903qoxl-d
Shazeb
Please check the screenshto
Bullet
Check block extra_head
Bullet
Could be repeating the same block in content
Bullet
Or .. the query..
Mamikon
Hi friends.. I have a question. I'm using rest_framework for my blog project and it works fine... Now I need to integrate it in browser web page, what would you suggest me, how can I do that? Sorry for bad English :)
Mamikon
I mean, Shell I use react js to fetch all API data... Is this correct way to create WEB SITE?
Ali Hasan
I mean, Shell I use react js to fetch all API data... Is this correct way to create WEB SITE?
Yes , you have a REST API and you can use any front end framework (react, vue, angular,…etc) or mobile as client using flutter , … etc
Massi
Hi guys, I've just created a Blog model in which I added, during a second migration, the field "author", but in order to do that I was forced to add a null=True to the parameters, like as follow: author = models.ForeignKey(..., null=True, ...) My question is: is it considered a good practice, or is there a better way?
𝙄𝙨𝙝𝙖𝙣 𝙎𝙞𝙣𝙜𝙡𝙖
Any way to use camera from brouser using flask
Sibi
Hello I am using swagger with django...it's working fine in local but showing blank page in production..any help
Doragonsureiyā
Any way to use camera from brouser using flask
Your question is not related to Django. If it's a Python related question go to @Python group, otherwise please move to @PythonOfftopic group and ask there.
S
how to update data in django without using ajax and form plz help
Дмитрий Борисов
how to update data in django without using ajax and form plz help
Use shell - import your model and use Django orm methods
S
can show me the example plz
Д
how to update data in django without using ajax and form plz help
https://lmgtfy.app/?q=how+to+update+data+in+django
Gordey
Hi i am having problem with cookiecutter-django CELERY_BROKER_URL and DATABASE_URL does not get set. How to fix? export CELERY_BROKER_URL="${REDIS_URL}" if [ -z "${POSTGRES_USER}" ]; then base_postgres_image_default_user='postgres' export POSTGRES_USER="${base_postgres_image_default_user}" fi export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
S Sidharth
Hi I was trying to restrict pages based upon a BooleanField created in a custom user model. Below is the view for the same: def loginpage(request): if request.method == 'POST': email = request.POST.get('email') password = request.POST.get('password') user = authenticate(request, email=email, password=password) if user is not None: login(request, user) if user.is_syssuprvsr: return redirect('sysspvsr') elif user.is_eeesuprvsr: return redirect('eeespvsr') elif user.is_othersyssuprvsr: return redirect('otherspvsr') else: return redirect('home') else: messages.info(request, 'Incorrect Email or Password',extra_tags='login') context = {} return render(request, 'login.html', context) Here, say sysspvsr is logged in and they are redirected to the so called sysspvsr url. But when I press back button, then it is redirected to home. But I want the person to stay at sysspvsr even they press back button
S Sidharth
how can i achieve it? thanks
S Sidharth
Д
Sry for not mentioning...it's a template
I'm trying to get the grasp of the redirection; I mean it's a frontend question but given that you're using templates, it needs to be handled in Django
Д
because if you were doing that purely with DRF, then the whole question would be transfered 100% out of Django
Д
trying to deal with Frontend problems in the Backend. have fun :D
😒😒
Hello I have multiple formsets data saved to my db by different users So in the edit form, I need to build a formset and retrieve all data for edit belonging to a logged in user iform = inlineformset_factory(parent_model=Sub, model=Item, form=updateItemForm, can_delete=True, fk_name='sub_category') itemform = iform(request.POST or None, instance=Sub.objects.get(id=subobj.values('id')[0]['id']) This can return only 1 instance I wish to generate all available forms If I create a loop, I can loop through all the formset in view but I can get all inside the template no matter how much I tried
😒😒
Please I really need assistance on this
Д
Please I really need assistance on this
want to know the secret sauce?
Д
Anything
this works every time: Keep it Stupid Simple.
Д
start from the simplest case. then write line by line. — the key is to keep it TOO simple.
Joseph
How do you precalculate a model field Eg I have a price field and discount field,, how do I set the discount field to a percentage of the price field when a price is entered,. I want both fields to be created at the same time
Cramer
Or use signals(presave and postsave)
Farzin
Hi everyone, when I call save() on serializer inherited class it gives me error I don't know why, I've tried to debug it but no result, my code and error are in in pasebin link below: https://pastebin.com/isgTvQaY
Дмитрий Борисов
try - filterType = validated_data.pop("filter_type", None)
Anonymous
Guys. We create the superuser only once in a project?
Denis
Дмитрий Борисов
Where should I use this?
My previous answer was wrong. I guess in line 263 you initiate self.Meta.model and pass into **self.filters(validated_data) - this construction return {**filters, "filterType": validated_data[filterType]} - check key - filterType - it seems to me that your model doesnt have this field but you pass it - and therefore you get this exception
Anonymous
Any reason you need more than one?
Idk, are there any situations where you would need?
Denis
Idk, are there any situations where you would need?
I didn’t come across any in my projects.