jūdê
you are programmer
Yes A backend engineer For the sake of understanding what goes on in the framework as you develop I’ll advise you go with Corey Schafer He tends to use simple examples to give you a picture of what goes on under the hood and why you should follow certain rules over other things. It’s better you start off learning certain rules so that you don’t have to unlearn a lot in the future but always keep an open mind.
jūdê
Can anyone explain how is annotate() and aggregate() connected
Annotate() performs an operation for each object of a queryset. Aggregate() performs an operation over all the objects of a queryset. So if you do a count using Count for lets say, a field in a model over a queryset: If you use annotate, it would ascribe that count of the field of each object to each object. But if you use aggregate, it would return the count of the field of all the objects. Annotate would return a Queryset with the ascribed values. Aggregate would return the summary as requested.
jūdê
Let me find an article for you
jūdê
Let me find an article for you
https://stackoverflow.com/questions/7981837/aggregate-vs-annotate-in-django https://www.scaler.com/topics/django/aggregating-and-annotating-objects/ Then the docs gives a lot of examples too https://docs.djangoproject.com/en/4.2/topics/db/aggregation/
.
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 13: 'else', expected 'endblock'. Did you forget to register or load this tag ?
.
i have a problem what is this
.
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 13: 'else', expected 'endblock'. Did you forget to register or load this tag ?
{% extends 'tasks/base.html' %} {% block content %} {% if tasks %} {% for task in tasks %} <div> <h2>{{task.title}}</h2><br> <p>Created: {{task.created_at}}</p> <a href="{% url 'tasks:task_detail' task.pk %}">View</a> </div> {% endfor %} {% endif %} {% else %} <p>There is no task!</p> {% endblock %}
Kuda
Else first
Kuda
Then endif
.
{% extends 'tasks/base.html' %} 2 3 {% block content %} 4 {% if tasks %} 5 {% for task in tasks %} 6 <div> 7 <h2>{{task.title}}</h2><br> 8 <p>Created: {{task.created_at}}</p> 9 <a href="{% url 'tasks:task_detail' task.pk %}">View</a> 10 </div> 11 {% endfor %} 12 {% else %} 13 {% endif %} 14 <p>There is no task!</p>
.
it don't work
Vitt
What's your else doing here?
.
{% if tasks %}
.
that is problem
Vitt
{% if tasks %}
ifnotnull Oh staff like this
Vitt
I don't remember Jinja exactly
.
ifnotnull Oh staff like this
ohh thank you it workew
Jayashree
Helle everyone, How to save data to the database by using the database name.? I have used serializer.save(using=database_name) but before this line I have used serializer.is_valid(raise_exception = True). This line will throw an error (1146, "Table 'testdb.brand' doesn't exist") but the brand table is in the testdb_1 database
Rupesh
You are save data to database using djangorestfremwork
Rupesh
Use api
Jayashree
I have used
Jayashree
Still it will not save.
Jayashree
Serializer.is_valid() will throw an error
jūdê
Serializer.is_valid() will throw an error
So this line throws the database error?
jūdê
Yea
The serializer is a ModelSerializer The model serializer would go the model and I think that’s where the problem is
jūdê
The serializer.is_valid is probably calling the Model manager in some way(I’m not sure about it) and the model manager most likely goes to the default DB. So the only thing I can think of now is that maybe you should create a custom manager that indicates the database it is using for that model? Or Override the validation method of the serializer to fit your needs, maybe validate all the fields yourself in a pythonic way
jūdê
Use this stackoverflow post to see if it helps you in anyway https://stackoverflow.com/questions/70887881/django-drf-post-for-multiple-database-connections
Anonymous
Does anyone know about mssql configuration on python django4?
Anonymous
I installed mssql-django
Neha
How to use order by with distinct in django orm?
Kumar Shreyansh
You need to use PyODBC package to connect with any other SQL server
Kumar Shreyansh
The current packages exist for sqlite, MySQL, Postgres, Oracle
Boi
How to use order by with distinct in django orm?
Variable =Classname.objects.values('columnname').distinct()
jūdê
Okay I'll check. Thank you
I’d suggest you use serializers.Serializer class for the deserialisation and validation And then using the model manager to update or create it manually: Foo.objects.db_manager(“database_name”).create() Or foo1 = Foo(**fields_value) foo1.save(using=“database_name”). Until you figure out a better way
Jayashree
I’d suggest you use serializers.Serializer class for the deserialisation and validation And then using the model manager to update or create it manually: Foo.objects.db_manager(“database_name”).create() Or foo1 = Foo(**fields_value) foo1.save(using=“database_name”). Until you figure out a better way
Okay. But... There are so many foreign keys. By using .create(**data), for each foreign key I need to create an instance and then re assign the value to the data. That will be a very long method. Before I was using the dynamic_db_router. By using this, the db connection will not close for each request and if there is an error it will throw an error but connection will not close. So "too many connections" issues occur. So removed dynamic_db_router. Now I'm passing the database_name as a parameter to the .save(using =).
Boi
I want to use order by too
Before/After distinct() just use order_by('colname')
ТинМин
from myapp.views import indexImportError: cannot import name 'index' from 'myapp.views' (E:\pi\coolsite\myapp\views.py)
ТинМин
I dont nou
ТинМин
where eroorr
Maxim
where eroorr
probably there is no index in myapp.views
ТинМин
from django.urls import pathfrom myapp.views import index from myapp.views import index urlpatterns = [ path('', index, name='index'), ]
ТинМин
from django.shortcuts import render def index(request): if request.method == 'POST': index = request.POST.get('index') return render(request, 'myapp/index.html', {'index': index}) else: return render(request, 'myapp/index.html')
Jahongir
can you use so from .views import index
M. Hossein
Why post_save signal works in API views but not in admin page?
Mirco
M. Hossein
nope, no diff as it acts at model level
I have to override save_model method in admin page to save by myself to signal be called
.
<link rel="stylesheet" type="text/css" href="{% static 'static/css/page.css' %}">
.
How can i connect css with django
Виктор
Виктор
<link rel="stylesheet" type="text/css" href="{% static 'static/css/page.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/page.css' %}">
.
[17/Jul/2023 16:16:58] "GET /static/css/page.css HTTP/1.1" 404 1795 Error
Виктор
doen't work
Add "/" before "css"😄
Hanane
hello guys how can we solve this : The 'image' attribute has no file associated with it. ?
Hanane
i have this in my models.py : image = models.ImageField(blank=True,upload_to='images') and i checked under media there is a folder with the name images .. any idea what could be the issue ?
Hu
Hello I am getting a 405 "get method not allowed" when I try to make a post api request using Django rest framework How do resolve it
Askat
Okay
Could you send?
Fedor
Hello everyone, I need to deal with one very large project on Django, the task is to add two models, but I need to figure out where.
Charly
Also please edit your message help must be provided here not in private
Fedor
Charly
if only it were that simple)
As you provided 0 details is impossible to suggest something else
Fedor
The project is very, very big