Re: Performing raw sql queries in database

2020-06-26 Thread Cbh Bnh
Hi,
Perhaps this could help you:

I had to find how many versions of each "edge" object without using a db 
version column 
So basically for this kind of dataset:
'''
| ID  | edge_id  | geom  |
++=+===+
| 1   | 22  | y   |
| 2   | 22 | x|
| 3   | 11  | a  |
'''
I expected a queryset like
'''
[{'id':1, 'edge_id':22, 'geom':'y', 'counter': 2}, {'id':2, 
'edge_id':22, 'geom':'x', 'counter': 2}, {'id':3, 'edge_id':11, 
'geom':'aa', 'counter': 1}]
'''
So I used this kind of django request:
'''
from django.db.models import Count, IntegerField, OuterRef, Subquery

counted_edges = Edge.objects.values('edge_id').filter(
edge_id=OuterRef('edge_id'), 
).annotate(
counter=Count('edge_id')
).order_by()

annotated_edges = Edge.objects.all().annotate(
pg_counter=(Subquery(
counted_edges.values('counter'), 
output_field=IntegerField()
)),
 )
'''
So if I needed only the instances where my 'counter' is greater then 1 I 
think it will be done like this:
'''
# ...
annotated_edges.filter(pg_counter__gt=1)
'''
I don't know if there is any better solution with raw SQL but hope this can 
help
Le jeudi 25 juin 2020 à 16:55:54 UTC+2, tejasj...@gmail.com a écrit :

> Hi,
> I want to return the first occurrence of repetitive fields in my model.
> I have a title field in my model as follows :
>
> models.py 
> class Post(models.Model):
> title = models.CharField(max_length=20)
>
> There will be multiple 'Post' with the same title.
> I want to exectute the following query on the Post model
>
> Post.objects.raw("SELECT title,ROW_NUMBER() OVER(PARTITION BY title ORDER 
> BY title) AS row_num FROM basement_post;")
>
> I am getting a syntax error "("
> Is django not compatible with SQL server queries or am I missing something 
> ??
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/abd9bab6-29d0-4687-bb0e-194bf21eb490n%40googlegroups.com.


Re: I have posted my question in stackoverflow

2020-06-26 Thread Cbh Bnh

Hi,

I'm not sure about what you are expecting from your template "home.html"
but as it is rendered from your view "home(request)", the data passed came 
from the user instance that is logged-in (cf the decorator '@login_required') 
as it is explained here 
(https://docs.djangoproject.com/en/2.2/ref/templates/api/#django-contrib-auth-context-processors-auth)
 

This instance seems to be defined from the default user model (cf "from 
django.contrib.auth.models import User")
in that case your custom fields "Business_name" and "mobile" are not 
defined in the default user model
if you want such fields pleaseyou will need to extend this model cf 
https://docs.djangoproject.com/fr/2.2/topics/auth/customizing/#extending-the-existing-user-model
```
>home.html
{% block content %}
 {{ user.Business_name }} {{ user.first_name}} {{ user.last_name
}} {{ user.email }} {{user.username}} {{ user.mobile }} 
{% end block %}
 ```
Also in your stackoverflow message you show this UserCreationForm
If you actually want to use it in a template you actually need more work cf 
this quick note in this section: 
https://docs.djangoproject.com/fr/2.2/topics/forms/#working-with-forms

PS: keep your field name "Business_name" in it's lower case form 
'buisness_name' as it's a variable and not a class

Hope this gave you some help

Cheers

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6d5193b2-4e39-4143-ba8c-b05020bebd51n%40googlegroups.com.


Re: Django Reference Manual on what classes can be used with views, urls, etc...

2020-06-24 Thread Cbh Bnh
hi,
Have a look to this site https://devdocs.io/django~2.2/
They have the same content then the official django doc but add a usefull 
search engine, links between sections and organise their content regarding 
the code structure and modules
Cheers
Le mercredi 24 juin 2020 à 02:44:46 UTC+2, dlt1...@gmail.com a écrit :

> I have been looking for a Django Reference Manual to let me know what 
> classes, or Djanago tools
>
> are allowed in its calling syntax...  Example
>
>
> from django.urls import path
> from MyApplication import views
>
>
> What I am looking for is an explanation of what things can be imported with 
> URLS, VIEWS, HTTP 
>
> There are other classes each can use, but I have not been able to find any 
> source or book 
>
> that outlines all the classes or commands that can be used in Django 
> statements using...
>
>  From django.xxx import.yyy.
>
>
> I hope I am making myself understood.
>
>
> Does anyone have suggestions on sites, documents, or books that I can use as 
> a reference source?
>
>
> Thanks in advance for fielding my newbie question.
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/092bc768-afb0-4c3e-b04f-70bc8fe67678n%40googlegroups.com.


Re: Django Reference Manual on what classes can be used with views, urls, etc...

2020-06-24 Thread Cbh Bnh
Hi,
I don't know if this will help but from time to time I use this site to 
browse into django doc: https://devdocs.io/django~2.2/
I think they use the same content as the offcial documentation but with  
better links and quit good search tool

Cheers

Le mercredi 24 juin 2020 à 02:44:46 UTC+2, dlt1...@gmail.com a écrit :

> I have been looking for a Django Reference Manual to let me know what 
> classes, or Djanago tools
>
> are allowed in its calling syntax...  Example
>
>
> from django.urls import path
> from MyApplication import views
>
>
> What I am looking for is an explanation of what things can be imported with 
> URLS, VIEWS, HTTP 
>
> There are other classes each can use, but I have not been able to find any 
> source or book 
>
> that outlines all the classes or commands that can be used in Django 
> statements using...
>
>  From django.xxx import.yyy.
>
>
> I hope I am making myself understood.
>
>
> Does anyone have suggestions on sites, documents, or books that I can use as 
> a reference source?
>
>
> Thanks in advance for fielding my newbie question.
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/47a92d21-9f00-47bc-9219-320a3419a7bbn%40googlegroups.com.