Friday, April 30, 2021

override save() twice in django model!

am i able to override save() method of a django model twice?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b56f7397-66c1-44e5-8502-a83717861d54n%40googlegroups.com.

Send PDF as an email's attachment ( Working with xhtml2pdf library)

Hi Dudes, Im working with pisa from xhtml2pdf LIB in order to get a pdf. That works OK, but i cant figure out how to attach the generated pdf to  an email  in order to send it . Anyone dealt with this? I would appreciate any help on this!

View:

  def get(self,request,*args,**kwargs):
        try:
            mailServer = smtplib.SMTP(settings.EMAIL_HOST,settings.EMAIL_PORT)
            print(mailServer.ehlo())
            mailServer.starttls()
            print(mailServer.ehlo())
            mailServer.login(settings.EMAIL_HOST_USER,settings.EMAIL_HOST_PASSWORD)
            print("conectando...")
            email_to=[]
            email_to.append("emailtosenditto@gmail.com")
            subject="ADICRA - Comprobrante de Pago"
            template= get_template('pagos/invoice.html')
            context={
                'sale': PagosHead.objects.get(pk=self.kwargs['pk']),
                'comp': {'name':'ADICRA','ruc':'Av. Directorio Adicra 101','address':'C.A.B.A.'},
                'icon': '{}{}'.format(settings.STATIC_URL,'core/img/adicrareng.jpg'),
                'equis':'{}{}'.format(settings.STATIC_URL,'pagos/img/x.jpg'),            
                }
            html=template.render(context)
            response= HttpResponse(content_type='application/pdf')
            #response['Content-Disposition']='attachment; filename="pago.pdf"' #Para Descargar
            pisaStatus=pisa.CreatePDF(html, dest=response,link_callback=self.link_callback)
            mensaje = EmailMessage(subject, body=pdf, from_email=settings.EMAIL_HOST_USER, to=email_to)
            print("Acá antes del attach")
            mensaje.attach('pago.pdf', pisaStatus,'application/pdf')
            mensaje.content_subtype = "pdf"  
            mensaje.encoding = 'us-ascii'
            mensaje.send()
            #mailServer.sendmail(settings.EMAIL_HOST_USER,
             #       email_to,
              #      mensaje.as_string())
            print("Correo enviado correcamente")
            return response
        except Exception as e:
            print(e)
        return HttpResponse(reverse_lazy('pagos:list'))

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55b59d75-82de-4250-b180-2f0772e44213n%40googlegroups.com.

Python/Django job in energy efficiency

Want to put your python/django skills to work helping to reduce carbon footprints throughout the energy sector? Energy Solutions is looking for an experienced developer 
able to work permanent/full-time in one of the 50 U.S. states.  (Sorry - small company.)  Need about 4 years of experience - sorry no new grads for these roles, but we do sometimes hire interns or entry level, just not right now.

https://www.python.org/jobs/5344/

Great team of passionate, skilled developers here, and we really are making a dent in energy consumption in many areas. Let's work together! 

Scot
(please respond via the link above, not to me)

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/95c90cd8-9a1d-4dfb-b0e8-f84f72ac6ee0n%40googlegroups.com.

Re: Loading csv data in database

On Friday, 30 April, 2021, 08:28:07 pm IST, Derek <gamesbook@gmail.com> wrote:


Someone has an example of doing just this:

https://arshovon.com/blog/django-custom-command/


On Thursday, 29 April 2021 at 19:39:36 UTC+2 Ryan Nowakowski wrote:
Typically you would write a custom management command for this kind of thing:

https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/


On April 29, 2021 11:05:29 AM CDT, 'Muhammad Asim Khaskheli' via Django users <django...@googlegroups.com> wrote:

Hi,

I wanted to ask how to load CSV data into the model. What steps do we have to follow?

I have made this function but how exactly to run this code because, python manage.py load data won't work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)

Now, how to run this script?


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/137aee79-07e6-40e1-badd-671ae3b0c87cn%40googlegroups.com
.

Re: Loading csv data in database

Someone has an example of doing just this:

https://arshovon.com/blog/django-custom-command/


On Thursday, 29 April 2021 at 19:39:36 UTC+2 Ryan Nowakowski wrote:
Typically you would write a custom management command for this kind of thing:

https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/


On April 29, 2021 11:05:29 AM CDT, 'Muhammad Asim Khaskheli' via Django users <django...@googlegroups.com> wrote:

Hi,

I wanted to ask how to load CSV data into the model. What steps do we have to follow?

I have made this function but how exactly to run this code because, python manage.py load data won't work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)

Now, how to run this script?


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/137aee79-07e6-40e1-badd-671ae3b0c87cn%40googlegroups.com.

Re: Filtering OR-combined queries

Hi,

On Fri, 30 Apr 2021 at 11:52, Sebastian Jung <sebastian.jung2@gmail.com> wrote:

Thanks for the response, but the OR is not the problem: it works just fine. (FWIW, I used the "|" form rather than the Q() form because it is not clear to me how one can express a .exclude() when using Q()). The problem here is that the result of the OR cannot be reliably .filter()d.

As I tried to explain, I can see that in some cases, a .filter() of the OR'd result does indeed add extra SQL to the query, whereas in others it does not:

    qs_or_result.filter(a='b')

This works for some values of "a" but not others, I can use this to achieve the same effect:

    Buss.objects.filter(a='b').intersection(qs_or_result)

though it is rather clumsy!!! FWIW, this is with Django 3.2. I'm inclined to think this is a bug in the ORM, though I have not dived into the code to track it down.

Thanks, Shaheed
 


Shaheed Haque <shaheedhaque@gmail.com> schrieb am Fr., 30. Apr. 2021, 02:43:
Hi,

I have a query which ORs some selects on a single table together like this:

jurisdiction = 'aaa'
qs = Buss.objects.filter(jurisdiction=jurisdiction)
qs = qs | Buss.objects.filter(jurisdiction='xxx').exclude(name__in=qs.values_list('name', flat=True))
qs = qs | Buss.objects.filter(jurisdiction='yyy').exclude(name__in=qs.values_list('name', flat=True))

This seems to work just fine (and the raw SQL looks suitably complicated):

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

Now, if I post-filter qs using something other than "aaa" (the first term above) like this:

   qs.filter('xxx)

then the resulting SQL has an 'AND "paiyroll_buss"."jurisdiction" = xxx' as one might expect. However, if I try to post-filter qs like this:

    qs.filter('aaa') # 'aaa' was the first term in the original trio of clauses

Then the formed SQL looks like this:

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

i.e. just like the original 3-clause query. (I realise that in this case, the query degenerates to the first term). What am I missing?

Any clues appreciated.

Thanks, Shaheed




--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2jcLVcFFiXRZtX_iGTJ2YJ0CNsObRFDyw1Ss6i3nV-X-vw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9mwaiSjyrrMJfrvoXMShfnqjxhonkHTbyOVdCGqJwqK85w%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2jdWB-9FLJWZbcjuEp53-M9jGrv7YUN_hecWa4w4Nzkh%2BQ%40mail.gmail.com.

Re: Filtering OR-combined queries


Shaheed Haque <shaheedhaque@gmail.com> schrieb am Fr., 30. Apr. 2021, 02:43:
Hi,

I have a query which ORs some selects on a single table together like this:

jurisdiction = 'aaa'
qs = Buss.objects.filter(jurisdiction=jurisdiction)
qs = qs | Buss.objects.filter(jurisdiction='xxx').exclude(name__in=qs.values_list('name', flat=True))
qs = qs | Buss.objects.filter(jurisdiction='yyy').exclude(name__in=qs.values_list('name', flat=True))

This seems to work just fine (and the raw SQL looks suitably complicated):

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

Now, if I post-filter qs using something other than "aaa" (the first term above) like this:

   qs.filter('xxx)

then the resulting SQL has an 'AND "paiyroll_buss"."jurisdiction" = xxx' as one might expect. However, if I try to post-filter qs like this:

    qs.filter('aaa') # 'aaa' was the first term in the original trio of clauses

Then the formed SQL looks like this:

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

i.e. just like the original 3-clause query. (I realise that in this case, the query degenerates to the first term). What am I missing?

Any clues appreciated.

Thanks, Shaheed




--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2jcLVcFFiXRZtX_iGTJ2YJ0CNsObRFDyw1Ss6i3nV-X-vw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9mwaiSjyrrMJfrvoXMShfnqjxhonkHTbyOVdCGqJwqK85w%40mail.gmail.com.

Re: I have one doubt

Two things that you might want to check.

1.Ensure you have activated the virtualenv (if you running in the virtualenv mode)
2. Ensure your system has the python path set


On Friday, April 30, 2021 at 3:32:47 PM UTC+5:30 bheem...@gmail.com wrote:
I had installed

On Fri, 30 Apr, 2021, 15:17 , <yeddu.j...@gmail.com> wrote:

Hope you have the Installed_apps setup properly.

 

 

From: django...@googlegroups.com <django...@googlegroups.com> On Behalf Of Bheemanagowda S Gowdra
Sent: 30 April 2021 15:10
To: django...@googlegroups.com
Subject: Re: I have one doubt

 

 

On Fri, 30 Apr, 2021, 15:07 Arpana Mehta, <arpana...@gmail.com> wrote:

Can you share the error stack in more detail? Or a screenshot?

 

On Thu, 29 Apr 2021, 19:53 Bheemanagowda S Gowdra, <bheem...@gmail.com> wrote:

I am knew to Django

 

I am getting below warning 

 

unresolved import 'django.contrib' 

--
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/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

--
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/CAGyqUuVFS%3DEHmBCutp7OAq50q8t6R07FPbytQTfcQ53nEH2O%2Bw%40mail.gmail.com.

--
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/CAC7OE0tY_%3DVGhQ-D5YhwY-dBC541U1kcTXbb%3DJBJ7DtHXs6oCg%40mail.gmail.com.

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a96a1022-2a7c-4202-9f82-b0bd4e912a7fn%40googlegroups.com.

Re: I have one doubt

I had installed

On Fri, 30 Apr, 2021, 15:17 , <yeddu.j.prasad@gmail.com> wrote:

Hope you have the Installed_apps setup properly.

 

 

From: django-users@googlegroups.com <django-users@googlegroups.com> On Behalf Of Bheemanagowda S Gowdra
Sent: 30 April 2021 15:10
To: django-users@googlegroups.com
Subject: Re: I have one doubt

 

 

On Fri, 30 Apr, 2021, 15:07 Arpana Mehta, <arpana11mehta@gmail.com> wrote:

Can you share the error stack in more detail? Or a screenshot?

 

On Thu, 29 Apr 2021, 19:53 Bheemanagowda S Gowdra, <bheemu11007@gmail.com> wrote:

I am knew to Django

 

I am getting below warning 

 

unresolved import 'django.contrib' 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyqUuVFS%3DEHmBCutp7OAq50q8t6R07FPbytQTfcQ53nEH2O%2Bw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAC7OE0tY_%3DVGhQ-D5YhwY-dBC541U1kcTXbb%3DJBJ7DtHXs6oCg%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/003c01d73da5%24d6be4190%24843ac4b0%24%40gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAC7OE0sMS2kf1K%3D%3D-FMYy9i3jVj7mDfJM74qNEYRo_7k0gL70g%40mail.gmail.com.

RE: I have one doubt

Hope you have the Installed_apps setup properly.

 

 

From: django-users@googlegroups.com <django-users@googlegroups.com> On Behalf Of Bheemanagowda S Gowdra
Sent: 30 April 2021 15:10
To: django-users@googlegroups.com
Subject: Re: I have one doubt

 

 

On Fri, 30 Apr, 2021, 15:07 Arpana Mehta, <arpana11mehta@gmail.com> wrote:

Can you share the error stack in more detail? Or a screenshot?

 

On Thu, 29 Apr 2021, 19:53 Bheemanagowda S Gowdra, <bheemu11007@gmail.com> wrote:

I am knew to Django

 

I am getting below warning 

 

unresolved import 'django.contrib' 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyqUuVFS%3DEHmBCutp7OAq50q8t6R07FPbytQTfcQ53nEH2O%2Bw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAC7OE0tY_%3DVGhQ-D5YhwY-dBC541U1kcTXbb%3DJBJ7DtHXs6oCg%40mail.gmail.com.

Re: I have one doubt


On Fri, 30 Apr, 2021, 15:07 Arpana Mehta, <arpana11mehta@gmail.com> wrote:
Can you share the error stack in more detail? Or a screenshot?

On Thu, 29 Apr 2021, 19:53 Bheemanagowda S Gowdra, <bheemu11007@gmail.com> wrote:
I am knew to Django

I am getting below warning 

unresolved import 'django.contrib' 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyqUuVFS%3DEHmBCutp7OAq50q8t6R07FPbytQTfcQ53nEH2O%2Bw%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAC7OE0tY_%3DVGhQ-D5YhwY-dBC541U1kcTXbb%3DJBJ7DtHXs6oCg%40mail.gmail.com.

Re: I have one doubt

Can you share the error stack in more detail? Or a screenshot?

On Thu, 29 Apr 2021, 19:53 Bheemanagowda S Gowdra, <bheemu11007@gmail.com> wrote:
I am knew to Django

I am getting below warning 

unresolved import 'django.contrib' 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyqUuVFS%3DEHmBCutp7OAq50q8t6R07FPbytQTfcQ53nEH2O%2Bw%40mail.gmail.com.

Thursday, April 29, 2021

Filtering OR-combined queries

Hi,

I have a query which ORs some selects on a single table together like this:

jurisdiction = 'aaa'
qs = Buss.objects.filter(jurisdiction=jurisdiction)
qs = qs | Buss.objects.filter(jurisdiction='xxx').exclude(name__in=qs.values_list('name', flat=True))
qs = qs | Buss.objects.filter(jurisdiction='yyy').exclude(name__in=qs.values_list('name', flat=True))

This seems to work just fine (and the raw SQL looks suitably complicated):

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

Now, if I post-filter qs using something other than "aaa" (the first term above) like this:

   qs.filter('xxx)

then the resulting SQL has an 'AND "paiyroll_buss"."jurisdiction" = xxx' as one might expect. However, if I try to post-filter qs like this:

    qs.filter('aaa') # 'aaa' was the first term in the original trio of clauses

Then the formed SQL looks like this:

SELECT "paiyroll_buss"."id", "paiyroll_buss"."jurisdiction", "paiyroll_buss"."name", "paiyroll_buss"."description" FROM "paiyroll_buss" WHERE ("paiyroll_buss"."jurisdiction" = aaa OR ("paiyroll_buss"."jurisdiction" = xxx AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))) OR ("paiyroll_buss"."jurisdiction" = yyy AND NOT ("paiyroll_buss"."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE (U0."jurisdiction" = aaa OR (U0."jurisdiction" = xxx AND NOT (U0."name" IN (SELECT U0."name" FROM "paiyroll_buss" U0 WHERE U0."jurisdiction" = aaa))))))))

i.e. just like the original 3-clause query. (I realise that in this case, the query degenerates to the first term). What am I missing?

Any clues appreciated.

Thanks, Shaheed




--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2jcLVcFFiXRZtX_iGTJ2YJ0CNsObRFDyw1Ss6i3nV-X-vw%40mail.gmail.com.

Re: Loading csv data in database

Typically you would write a custom management command for this kind of thing:

https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/

On April 29, 2021 11:05:29 AM CDT, 'Muhammad Asim Khaskheli' via Django users <django-users@googlegroups.com> wrote:

Hi,

I wanted to ask how to load CSV data into the model. What steps do we have to follow?

I have made this function but how exactly to run this code because, python manage.py load data won't work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)

Now, how to run this script?


Re: Loading csv data in database

you may use pandas for this 
you can use it as follows:-
first install it
if not using python from anaconda environment!

pip install pandas
import pandas as pd
reader=pd.read_csv("sample-dataset.csv")
reader.head(3) # for viewing top 3 rows.

hope this may help you
 -)-)
On Thursday, 29 April 2021 at 21:43:28 UTC+5:30 mkhaskheli....@seecs.edu.pk wrote:

Hi,

I wanted to ask how to load CSV data into the model. What steps do we have to follow?

I have made this function but how exactly to run this code because, python manage.py load data won't work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)

Now, how to run this script?


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f9a6e8cc-2d34-4641-aa7e-9d4ead0696ben%40googlegroups.com.

Loading csv data in database


Hi,

I wanted to ask how to load CSV data into the model. What steps do we have to follow?

I have made this function but how exactly to run this code because, python manage.py load data won't work.

def import_data():
with open('sample-dataset.csv') as f:
reader = csv.reader(f)
for row in reader:
if row[0] != 'user_id':
_, created = Funnel.objects.get_or_create(
user_id=row[0],
event=row[1],
timestamp=row[2],
session_id=row[3]
)

Now, how to run this script?


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fefae867-8bc5-4d9a-a9e6-e0268d20632cn%40googlegroups.com.

I have one doubt

I am knew to Django

I am getting below warning 

unresolved import 'django.contrib' 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa706c85-e903-4c8c-a73d-0b4ddedc424bn%40googlegroups.com.

Wednesday, April 28, 2021

Re: hello dear sir i have one dout


On Wed, Apr 28, 2021 at 7:33 PM TOURIST PALACE <rupaksahoo1994@gmail.com> wrote:
i have one dout how use slug per post i didnot understood

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9c1158be-c973-4f2f-840a-3bfb46db40e8n%40googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAE59x8cWyZ9ZomQZ-06M8ee2jis2qJ2n8dBajTA7%3DZaL84yAMw%40mail.gmail.com.

Condition never met on process_response with common middleware

Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import re 

2from urllib.parse import urlparse 

3 

4from django.conf import settings 

5from django.core.exceptions import PermissionDenied 

6from django.core.mail import mail_managers 

7from django.http import HttpResponsePermanentRedirect 

8from django.urls import is_valid_path 

9from django.utils.deprecation import MiddlewareMixin 

10from django.utils.http import escape_leading_slashes 

11 

12 

13class CommonMiddleware(MiddlewareMixin): 

14 """ 

15 "Common" middleware for taking care of some basic operations: 

16 

17 - Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS 

18 

19 - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings, 

20 append missing slashes and/or prepends missing "www."s. 

21 

22 - If APPEND_SLASH is set and the initial URL doesn't end with a 

23 slash, and it is not found in urlpatterns, form a new URL by 

24 appending a slash at the end. If this new URL is found in 

25 urlpatterns, return an HTTP redirect to this new URL; otherwise 

26 process the initial URL as usual. 

27 

28 This behavior can be customized by subclassing CommonMiddleware and 

29 overriding the response_redirect_class attribute. 

30 """ 

31 

32 response_redirect_class = HttpResponsePermanentRedirect 

33 

34 def process_request(self, request): 

35 """ 

36 Check for denied User-Agents and rewrite the URL based on 

37 settings.APPEND_SLASH and settings.PREPEND_WWW 

38 """ 

39 

40 # Check for denied User-Agents 

41 user_agent = request.META.get('HTTP_USER_AGENT') 

42 if user_agent is not None: 

43 for user_agent_regex in settings.DISALLOWED_USER_AGENTS: 

44 if user_agent_regex.search(user_agent): 44 ↛ 43line 44 didn't jump to line 43, because the condition on line 44 was never false

45 raise PermissionDenied('Forbidden user agent') 

46 

47 # Check for a redirect based on settings.PREPEND_WWW 

48 host = request.get_host() 

49 must_prepend = settings.PREPEND_WWW and host and not host.startswith('www.') 

50 redirect_url = ('%s://www.%s' % (request.scheme, host)) if must_prepend else '' 

51 

52 # Check if a slash should be appended 

53 if self.should_redirect_with_slash(request): 

54 path = self.get_full_path_with_slash(request) 

55 else: 

56 path = request.get_full_path() 

57 

58 # Return a redirect if necessary 

59 if redirect_url or path != request.get_full_path(): 

60 redirect_url += path 

61 return self.response_redirect_class(redirect_url) 

62 

63 def should_redirect_with_slash(self, request): 

64 """ 

65 Return True if settings.APPEND_SLASH is True and appending a slash to 

66 the request path turns an invalid path into a valid one. 

67 """ 

68 if settings.APPEND_SLASH and not request.path_info.endswith('/'): 

69 urlconf = getattr(request, 'urlconf', None) 

70 if not is_valid_path(request.path_info, urlconf): 

71 match = is_valid_path('%s/' % request.path_info, urlconf) 

72 if match: 

73 view = match.func 

74 return getattr(view, 'should_append_slash', True) 

75 return False 

76 

77 def get_full_path_with_slash(self, request): 

78 """ 

79 Return the full path of the request with a trailing slash appended. 

80 

81 Raise a RuntimeError if settings.DEBUG is True and request.method is 

82 POST, PUT, or PATCH. 

83 """ 

84 new_path = request.get_full_path(force_append_slash=True) 

85 # Prevent construction of scheme relative urls. 

86 new_path = escape_leading_slashes(new_path) 

87 if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'): 

88 raise RuntimeError( 

89 "You called this URL via %(method)s, but the URL doesn't end " 

90 "in a slash and you have APPEND_SLASH set. Django can't " 

91 "redirect to the slash URL while maintaining %(method)s data. " 

92 "Change your form to point to %(url)s (note the trailing " 

93 "slash), or set APPEND_SLASH=False in your Django settings." % { 

94 'method': request.method, 

95 'url': request.get_host() + new_path, 

96 } 

97 ) 

98 return new_path 

99 

100 def process_response(self, request, response): 

101 """ 

102 When the status code of the response is 404, it may redirect to a path 

103 with an appended slash if should_redirect_with_slash() returns True. 

104 """ 

105 # If the given URL is "Not Found", then check if we should redirect to 

106 # a path with a slash appended. 

107 if response.status_code == 404 and self.should_redirect_with_slash(request): 107 ↛ 108line 107 didn't jump to line 108, because the condition on line 107 was never true

108 return self.response_redirect_class(self.get_full_path_with_slash(request)) 

109 

110 # Add the Content-Length header to non-streaming responses if not 

111 # already set. 

112 if not response.streaming and not response.has_header('Content-Length'): 

113 response.headers['Content-Length'] = str(len(response.content)) 

114 

115 return response 

116 

117 

118class BrokenLinkEmailsMiddleware(MiddlewareMixin): 

119 

120 def process_response(self, request, response): 

121 """Send broken link emails for relevant 404 NOT FOUND responses.""" 

122 if response.status_code == 404 and not settings.DEBUG: 122 ↛ 139line 122 didn't jump to line 139, because the condition on line 122 was never false

123 domain = request.get_host() 

124 path = request.get_full_path() 

125 referer = request.META.get('HTTP_REFERER', '') 

126 

127 if not self.is_ignorable_request(request, path, domain, referer): 

128 ua = request.META.get('HTTP_USER_AGENT', '<none>') 

129 ip = request.META.get('REMOTE_ADDR', '<none>') 

130 mail_managers( 

131 "Broken %slink on %s" % ( 

132 ('INTERNAL ' if self.is_internal_request(domain, referer) else ''), 

133 domain 

134 ), 

135 "Referrer: %s\nRequested URL: %s\nUser agent: %s\n" 

136 "IP address: %s\n" % (referer, path, ua, ip), 

137 fail_silently=True, 

138 ) 

139 return response 

140 

141 def is_internal_request(self, domain, referer): 

142 """ 

143 Return True if the referring URL is the same domain as the current 

144 request. 

145 """ 

146 # Different subdomains are treated as different domains. 

147 return bool(re.match("^https?://%s/" % re.escape(domain), referer)) 

148 

149 def is_ignorable_request(self, request, uri, domain, referer): 

150 """ 

151 Return True if the given request *shouldn't* notify the site managers 

152 according to project settings or in situations outlined by the inline 

153 comments. 

154 """ 

155 # The referer is empty. 

156 if not referer: 

157 return True 

158 

159 # APPEND_SLASH is enabled and the referer is equal to the current URL 

160 # without a trailing slash indicating an internal redirect. 

161 if settings.APPEND_SLASH and uri.endswith('/') and referer == uri[:-1]: 

162 return True 

163 

164 # A '?' in referer is identified as a search engine source. 

165 if not self.is_internal_request(domain, referer) and '?' in referer: 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never true

166 return True 

167 

168 # The referer is equal to the current URL, ignoring the scheme (assumed 

169 # to be a poorly implemented bot). 

170 parsed_referer = urlparse(referer) 

171 if parsed_referer.netloc in ['', domain] and parsed_referer.path == uri: 

172 return True 

173 

174 return any(pattern.search(uri) for pattern in settings.IGNORABLE_404_URLS) 


Hello, I'm Tidiane and I've been using Django for some months now.
I've been digging into the codebase these days to gain more familiriaty with the project and eventually contribute to the project.

I don't know if it's a bug or if there is something I am not getting right but here what I noticed:

I was looking at the coverage of the projects and found that this line is never hit which impacts the coverage of CommonMiddleware. I also attach the coverage report for the file.

Indeed the two conditions can never be met at the same time since the process_request method already handles if a slash should be appended.
When APPEND_SLASH is set to True, if we have a 404 error after process_request has been called, the condition should_redirect_with_slash can never be met.

So is there a need to do a check at the mentionned line or is there something Iam I not getting right?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/01b7df9e-ffb2-4015-be6b-27f7492a422en%40googlegroups.com.