Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Integr@te System
Hi All,

Let try sth like sample:
a = True if b <= c else False



On Tue, Jan 28, 2020, 03:24 Simon Charette  wrote:

> I see, I think you'll need to wrap the inner Q in an ExpressionWrapper[0]
>
> That would be
>
> CheckConstraint(check=Q(is_on_sale=ExpressionWrapper(Q(price__lt=F('full_price')),
> output_field=models.BooleanField(
>
> That involves a lot of boilerplate though and it ought to work without all
> of it so I'd submit an optimization ticket
> to allow direct usage of Q objects for boolean field lookup right hand
> sides
>
> Cheers,
> Simon
>
>
> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#django.db.models.ExpressionWrapper
>
> Le lundi 27 janvier 2020 14:32:24 UTC-5, Peter Law a écrit :
>>
>> Hi Simon,
>>
>> Thanks for your response.
>>
>> I did try that, however unfortunately I get an error when running the
>> migration:
>>
>> django.core.exceptions.ValidationError: ["'(AND:
>> )' value must
>> be either True or False."]
>>
>> I'm using Django 2.2 LTS though testing this in 3.0 unfortunately errors
>> in the same way.
>>
>> I think the issue with this spelling is that a `models.Q` isn't expecting
>> to be passed another `models.Q` as a value. I think ideally it would want
>> an expression there, which a `models.F` is closest to, however using a
>> `models.F` relation there also doesn't work (see my response to Stephen's
>> suggestion).
>>
>> Thanks,
>> Peter
>>
>> On Monday, 27 January 2020 18:23:28 UTC, Simon Charette wrote:
>>>
>>> Did you try
>>>
>>> class Item(Model):
>>> price = DecimalField()
>>> full_price = DecimalField()
>>> is_on_sale = BooleanField()
>>>
>>> class Meta:
>>> constraints = [
>>>
>>> CheckConstraint(check=Q(is_on_sale=Q(price__lt=F('full_price'
>>> ]
>>>
>>> I haven't tried it myself but I would expect it to work on Django 3.0.
>>>
>>> Cheers,
>>> Simon
>>>
>>> Le lundi 27 janvier 2020 12:47:37 UTC-5, Peter Law a écrit :

 Hi,

 Thanks for adding support for check constraints in Django 2.2, it's
 great to be able to move constraints into the model definitions.

 I've been trying to work out how to express a constraint which
 validates that the value of one field expresses a relation between two
 other fields, but can't find a nice way to do so.

 I've read through the docs and also found
 https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
 but haven't found a concise spelling.

 I've got a model like:

 ```
 class Item(Model):
 price = DecimalField()
 full_price = DecimalField()
 is_on_sale = BooleanField()
 ```

 I'd like to be able to express neatly that the `is_on_sale` boolean be
 true only when `price < full_price`.
 In Postgres I can express this as:

 ```
 ALTER TABLE item
 ADD CONSTRAINT is_on_sale_check
 CHECK (is_on_sale = (price < full_price))
 ```

 However in Django I can't find a way to express this directly.

 I did find a long spelling which essentially checks the True case and
 the False case explicitly and then ORs them together, however it's
 several lines of `models.Q` combinations and not at all clear about
 what the code is trying to achieve.

 Is there a concise way to do this sort of constraint? If not, would it
 be possible for Django to add support for it?

 Thanks,
 Peter

>>> --
> 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/5d9488a8-7e6d-4a96-b009-fd94feb9df86%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP5HUWosH07MsPYC8h%2Bh-3h7uSF%2Bdr2DYaBWQYAby3cQy%3D5QEA%40mail.gmail.com.


Re: Can't find image files

2020-01-27 Thread Antje Kazimiers
Hi,

I keep my images files within in a static folder, so

./MyProject/static/media/myimage.png

would be my path. In the template I make use of it like that:



It's explained here:

https://docs.djangoproject.com/en/3.0/howto/static-files/

also that you need to define your static folder in your settings file:
STATIC_URL = '/static/'

and that you might need to run collectstatic first, if your DEBUG
variable is set to false in your settings file.


Kind regards,
Antje


On 1/27/20 7:52 PM, Dick Arnold wrote:
> My nice, new Django application is going along fairly well,  although
> I have run into a couple of roadblocks and need some help. The first
> problem is my HTML  HTML statement:
>
> 
>
> Here's the error message I get on my terminal (command prompt) window:
>
> Not Found: /GAW.jpg
> [25/Jan/2020 11:51:50] "GET /GAW.jpg HTTP/1.1" 404 3481
>
> I read every thing I could find and most of them tell me that it is in
> a location relative to the "current folder", but I can find nothing
> that explains what is meant  by "current".  I thought it might be
> where my terminal prompt comes from.   It's in the highest level
> project folder. Tried that and still no luck.
> -- 
> 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/4dd2997f-0063-4300-b075-404f99569f8e%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/38aefe7f-45c7-02d7-91f7-674b94509d13%40gmail.com.


Can't find image files

2020-01-27 Thread Dick Arnold
My nice, new Django application is going along fairly well,  although I 
have run into a couple of roadblocks and need some help. The first problem 
is my HTML 

Here's the error message I get on my terminal (command prompt) window:

Not Found: /GAW.jpg
[25/Jan/2020 11:51:50] "GET /GAW.jpg HTTP/1.1" 404 3481

I read every thing I could find and most of them tell me that it is in a 
location relative to the "current folder", but I can find nothing that 
explains what is meant  by "current".  I thought it might be where my 
terminal prompt comes from.   It's in the highest level project folder. 
Tried that and still no luck.

-- 
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/4dd2997f-0063-4300-b075-404f99569f8e%40googlegroups.com.


Announcing CockroachDB support for Django ORM

2020-01-27 Thread 'Charlotte Dillon' via Django users
https://pypi.org/project/django-cockroachdb/

-- 
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/d743a051-2c5d-4868-b878-eecaa0b51d53%40googlegroups.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Simon Charette
I see, I think you'll need to wrap the inner Q in an ExpressionWrapper[0]

That would be

CheckConstraint(check=Q(is_on_sale=ExpressionWrapper(Q(price__lt=F('full_price')),
 
output_field=models.BooleanField(

That involves a lot of boilerplate though and it ought to work without all 
of it so I'd submit an optimization ticket
to allow direct usage of Q objects for boolean field lookup right hand sides

Cheers,
Simon

https://docs.djangoproject.com/en/3.0/ref/models/expressions/#django.db.models.ExpressionWrapper

Le lundi 27 janvier 2020 14:32:24 UTC-5, Peter Law a écrit :
>
> Hi Simon,
>
> Thanks for your response.
>
> I did try that, however unfortunately I get an error when running the 
> migration:
>
> django.core.exceptions.ValidationError: ["'(AND: 
> )' value must 
> be either True or False."]
>
> I'm using Django 2.2 LTS though testing this in 3.0 unfortunately errors 
> in the same way.
>
> I think the issue with this spelling is that a `models.Q` isn't expecting 
> to be passed another `models.Q` as a value. I think ideally it would want 
> an expression there, which a `models.F` is closest to, however using a 
> `models.F` relation there also doesn't work (see my response to Stephen's 
> suggestion).
>
> Thanks,
> Peter
>
> On Monday, 27 January 2020 18:23:28 UTC, Simon Charette wrote:
>>
>> Did you try
>>
>> class Item(Model):
>> price = DecimalField()
>> full_price = DecimalField()
>> is_on_sale = BooleanField()
>>
>> class Meta:
>> constraints = [
>> 
>> CheckConstraint(check=Q(is_on_sale=Q(price__lt=F('full_price'
>> ]
>>
>> I haven't tried it myself but I would expect it to work on Django 3.0.
>>
>> Cheers,
>> Simon
>>
>> Le lundi 27 janvier 2020 12:47:37 UTC-5, Peter Law a écrit :
>>>
>>> Hi, 
>>>
>>> Thanks for adding support for check constraints in Django 2.2, it's 
>>> great to be able to move constraints into the model definitions. 
>>>
>>> I've been trying to work out how to express a constraint which 
>>> validates that the value of one field expresses a relation between two 
>>> other fields, but can't find a nice way to do so. 
>>>
>>> I've read through the docs and also found 
>>> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion, 
>>> but haven't found a concise spelling. 
>>>
>>> I've got a model like: 
>>>
>>> ``` 
>>> class Item(Model): 
>>> price = DecimalField() 
>>> full_price = DecimalField() 
>>> is_on_sale = BooleanField() 
>>> ``` 
>>>
>>> I'd like to be able to express neatly that the `is_on_sale` boolean be 
>>> true only when `price < full_price`. 
>>> In Postgres I can express this as: 
>>>
>>> ``` 
>>> ALTER TABLE item 
>>> ADD CONSTRAINT is_on_sale_check 
>>> CHECK (is_on_sale = (price < full_price)) 
>>> ``` 
>>>
>>> However in Django I can't find a way to express this directly. 
>>>
>>> I did find a long spelling which essentially checks the True case and 
>>> the False case explicitly and then ORs them together, however it's 
>>> several lines of `models.Q` combinations and not at all clear about 
>>> what the code is trying to achieve. 
>>>
>>> Is there a concise way to do this sort of constraint? If not, would it 
>>> be possible for Django to add support for it? 
>>>
>>> Thanks, 
>>> Peter 
>>>
>>

-- 
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/5d9488a8-7e6d-4a96-b009-fd94feb9df86%40googlegroups.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Peter Law
Hi Simon,

Thanks for your response.

I did try that, however unfortunately I get an error when running the 
migration:

django.core.exceptions.ValidationError: ["'(AND: 
)' value must 
be either True or False."]

I'm using Django 2.2 LTS though testing this in 3.0 unfortunately errors in 
the same way.

I think the issue with this spelling is that a `models.Q` isn't expecting 
to be passed another `models.Q` as a value. I think ideally it would want 
an expression there, which a `models.F` is closest to, however using a 
`models.F` relation there also doesn't work (see my response to Stephen's 
suggestion).

Thanks,
Peter

On Monday, 27 January 2020 18:23:28 UTC, Simon Charette wrote:
>
> Did you try
>
> class Item(Model):
> price = DecimalField()
> full_price = DecimalField()
> is_on_sale = BooleanField()
>
> class Meta:
> constraints = [
> 
> CheckConstraint(check=Q(is_on_sale=Q(price__lt=F('full_price'
> ]
>
> I haven't tried it myself but I would expect it to work on Django 3.0.
>
> Cheers,
> Simon
>
> Le lundi 27 janvier 2020 12:47:37 UTC-5, Peter Law a écrit :
>>
>> Hi, 
>>
>> Thanks for adding support for check constraints in Django 2.2, it's 
>> great to be able to move constraints into the model definitions. 
>>
>> I've been trying to work out how to express a constraint which 
>> validates that the value of one field expresses a relation between two 
>> other fields, but can't find a nice way to do so. 
>>
>> I've read through the docs and also found 
>> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion, 
>> but haven't found a concise spelling. 
>>
>> I've got a model like: 
>>
>> ``` 
>> class Item(Model): 
>> price = DecimalField() 
>> full_price = DecimalField() 
>> is_on_sale = BooleanField() 
>> ``` 
>>
>> I'd like to be able to express neatly that the `is_on_sale` boolean be 
>> true only when `price < full_price`. 
>> In Postgres I can express this as: 
>>
>> ``` 
>> ALTER TABLE item 
>> ADD CONSTRAINT is_on_sale_check 
>> CHECK (is_on_sale = (price < full_price)) 
>> ``` 
>>
>> However in Django I can't find a way to express this directly. 
>>
>> I did find a long spelling which essentially checks the True case and 
>> the False case explicitly and then ORs them together, however it's 
>> several lines of `models.Q` combinations and not at all clear about 
>> what the code is trying to achieve. 
>>
>> Is there a concise way to do this sort of constraint? If not, would it 
>> be possible for Django to add support for it? 
>>
>> Thanks, 
>> Peter 
>>
>

-- 
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/8bcd001f-6b56-49ac-bb0d-d41f4a37b76c%40googlegroups.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Peter Law
Hi Stephen,

Thanks for your response.

I agree that using an annotation might be an alternative way to solve this, 
however for various reasons that's not suitable for my current use case. I 
was also hoping for a general solution which would work for cases where the 
expression is more complicated (and thus where an annotation might be less 
desirable).

I'm using Django 2.2 LTS (though I can't see anything to suggest this has 
changed in 3.0) but your suggested annotation unfortunately doesn't work -- 
I get `TypeError: unorderable types: F() < F()`.

Thanks,
Peter

On Monday, 27 January 2020 17:59:21 UTC, Stephen J. Butler wrote:
>
> Frankly, if is_on_sale has such a tight constraint I wouldn't have it as 
> its own column. Why not just make it an annotated field with an F 
> expression?
>
>
> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations
>
> Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))
>
>
>
> On Mon, Jan 27, 2020 at 11:47 AM Peter Law > 
> wrote:
>
>> Hi,
>>
>> Thanks for adding support for check constraints in Django 2.2, it's
>> great to be able to move constraints into the model definitions.
>>
>> I've been trying to work out how to express a constraint which
>> validates that the value of one field expresses a relation between two
>> other fields, but can't find a nice way to do so.
>>
>> I've read through the docs and also found
>> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
>> but haven't found a concise spelling.
>>
>> I've got a model like:
>>
>> ```
>> class Item(Model):
>> price = DecimalField()
>> full_price = DecimalField()
>> is_on_sale = BooleanField()
>> ```
>>
>> I'd like to be able to express neatly that the `is_on_sale` boolean be
>> true only when `price < full_price`.
>> In Postgres I can express this as:
>>
>> ```
>> ALTER TABLE item
>> ADD CONSTRAINT is_on_sale_check
>> CHECK (is_on_sale = (price < full_price))
>> ```
>>
>> However in Django I can't find a way to express this directly.
>>
>> I did find a long spelling which essentially checks the True case and
>> the False case explicitly and then ORs them together, however it's
>> several lines of `models.Q` combinations and not at all clear about
>> what the code is trying to achieve.
>>
>> Is there a concise way to do this sort of constraint? If not, would it
>> be possible for Django to add support for it?
>>
>> Thanks,
>> Peter
>>
>> -- 
>> 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...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/bef957f3-a250-48ac-b102-fc0173201f83%40googlegroups.com.


RES: Django 3 material

2020-01-27 Thread Murilo A. Gigliotti
Hi Nathaniel,

 

The best way is the oficial project documentation website.

 

  https://docs.djangoproject.com/en/3.0/

 

Regards,

 

ASSINATURA DE EMAIL - GTI TECH v2

Odoo icone2 Odoo Business Partner

Mais informações visite http://www.gigliottitech.com.br/

 

De: django-users@googlegroups.com [mailto:django-users@googlegroups.com] Em 
nome de Nathaniel Brown
Enviada em: segunda-feira, 27 de janeiro de 2020 13:12
Para: django-users@googlegroups.com
Assunto: Django 3 material

 

I am looking for the best Django 3.0 books or learning material.

 

Any help would be greatly appreciated.

 

 

-- 
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/CAN_i64%2Bob3hLtwY4SQcTAHyEkDLAVkQZpYdG_48vC4uzesejgg%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/017f01d5d540%24daa50a10%248fef1e30%24%40com.br.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Simon Charette
Did you try

class Item(Model):
price = DecimalField()
full_price = DecimalField()
is_on_sale = BooleanField()

class Meta:
constraints = [

CheckConstraint(check=Q(is_on_sale=Q(price__lt=F('full_price'
]

I haven't tried it myself but I would expect it to work on Django 3.0.

Cheers,
Simon

Le lundi 27 janvier 2020 12:47:37 UTC-5, Peter Law a écrit :
>
> Hi, 
>
> Thanks for adding support for check constraints in Django 2.2, it's 
> great to be able to move constraints into the model definitions. 
>
> I've been trying to work out how to express a constraint which 
> validates that the value of one field expresses a relation between two 
> other fields, but can't find a nice way to do so. 
>
> I've read through the docs and also found 
> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion, 
> but haven't found a concise spelling. 
>
> I've got a model like: 
>
> ``` 
> class Item(Model): 
> price = DecimalField() 
> full_price = DecimalField() 
> is_on_sale = BooleanField() 
> ``` 
>
> I'd like to be able to express neatly that the `is_on_sale` boolean be 
> true only when `price < full_price`. 
> In Postgres I can express this as: 
>
> ``` 
> ALTER TABLE item 
> ADD CONSTRAINT is_on_sale_check 
> CHECK (is_on_sale = (price < full_price)) 
> ``` 
>
> However in Django I can't find a way to express this directly. 
>
> I did find a long spelling which essentially checks the True case and 
> the False case explicitly and then ORs them together, however it's 
> several lines of `models.Q` combinations and not at all clear about 
> what the code is trying to achieve. 
>
> Is there a concise way to do this sort of constraint? If not, would it 
> be possible for Django to add support for it? 
>
> Thanks, 
> Peter 
>

-- 
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/93fe186a-4f25-4ec2-ac5c-f28fd882812f%40googlegroups.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread DANIEL URBANO DE LA RUA
sorry i diin't read to Stephen his trik is even better hahahaha but with
sigñal yo ca do other thing

El lun., 27 ene. 2020 a las 19:02, DANIEL URBANO DE LA RUA (<
dannybombas...@gmail.com>) escribió:

> you can do few thing at the same time in a model before save or whenever
> you want
>
> El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<
> dannybombas...@gmail.com>) escribió:
>
>> it is goin to be better and less cost for the db
>>
>> El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<
>> dannybombas...@gmail.com>) escribió:
>>
>>> take a look at this
>>> https://docs.djangoproject.com/en/3.0/topics/signals/
>>>
>>>
>>> El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<
>>> stephen.but...@gmail.com>) escribió:
>>>
 Frankly, if is_on_sale has such a tight constraint I wouldn't have it
 as its own column. Why not just make it an annotated field with an F
 expression?


 https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations

 Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))



 On Mon, Jan 27, 2020 at 11:47 AM Peter Law  wrote:

> Hi,
>
> Thanks for adding support for check constraints in Django 2.2, it's
> great to be able to move constraints into the model definitions.
>
> I've been trying to work out how to express a constraint which
> validates that the value of one field expresses a relation between two
> other fields, but can't find a nice way to do so.
>
> I've read through the docs and also found
> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
> but haven't found a concise spelling.
>
> I've got a model like:
>
> ```
> class Item(Model):
> price = DecimalField()
> full_price = DecimalField()
> is_on_sale = BooleanField()
> ```
>
> I'd like to be able to express neatly that the `is_on_sale` boolean be
> true only when `price < full_price`.
> In Postgres I can express this as:
>
> ```
> ALTER TABLE item
> ADD CONSTRAINT is_on_sale_check
> CHECK (is_on_sale = (price < full_price))
> ```
>
> However in Django I can't find a way to express this directly.
>
> I did find a long spelling which essentially checks the True case and
> the False case explicitly and then ORs them together, however it's
> several lines of `models.Q` combinations and not at all clear about
> what the code is trying to achieve.
>
> Is there a concise way to do this sort of constraint? If not, would it
> be possible for Django to add support for it?
>
> Thanks,
> Peter
>
> --
> 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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_yRT1vDfqo2vRtmgUcwzc9FndWAgd3qZkxvxYptSFvcjDNKg%40mail.gmail.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread DANIEL URBANO DE LA RUA
you can do few thing at the same time in a model before save or whenever
you want

El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<
dannybombas...@gmail.com>) escribió:

> it is goin to be better and less cost for the db
>
> El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<
> dannybombas...@gmail.com>) escribió:
>
>> take a look at this https://docs.djangoproject.com/en/3.0/topics/signals/
>>
>>
>> El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<
>> stephen.but...@gmail.com>) escribió:
>>
>>> Frankly, if is_on_sale has such a tight constraint I wouldn't have it as
>>> its own column. Why not just make it an annotated field with an F
>>> expression?
>>>
>>>
>>> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations
>>>
>>> Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))
>>>
>>>
>>>
>>> On Mon, Jan 27, 2020 at 11:47 AM Peter Law  wrote:
>>>
 Hi,

 Thanks for adding support for check constraints in Django 2.2, it's
 great to be able to move constraints into the model definitions.

 I've been trying to work out how to express a constraint which
 validates that the value of one field expresses a relation between two
 other fields, but can't find a nice way to do so.

 I've read through the docs and also found
 https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
 but haven't found a concise spelling.

 I've got a model like:

 ```
 class Item(Model):
 price = DecimalField()
 full_price = DecimalField()
 is_on_sale = BooleanField()
 ```

 I'd like to be able to express neatly that the `is_on_sale` boolean be
 true only when `price < full_price`.
 In Postgres I can express this as:

 ```
 ALTER TABLE item
 ADD CONSTRAINT is_on_sale_check
 CHECK (is_on_sale = (price < full_price))
 ```

 However in Django I can't find a way to express this directly.

 I did find a long spelling which essentially checks the True case and
 the False case explicitly and then ORs them together, however it's
 several lines of `models.Q` combinations and not at all clear about
 what the code is trying to achieve.

 Is there a concise way to do this sort of constraint? If not, would it
 be possible for Django to add support for it?

 Thanks,
 Peter

 --
 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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_yRT2MBbAA5J2GX%2B8%2Bjy-W%3Dirk-XBSQNVD7_AEczWTE2492g%40mail.gmail.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread DANIEL URBANO DE LA RUA
take a look at this https://docs.djangoproject.com/en/3.0/topics/signals/


El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<
stephen.but...@gmail.com>) escribió:

> Frankly, if is_on_sale has such a tight constraint I wouldn't have it as
> its own column. Why not just make it an annotated field with an F
> expression?
>
>
> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations
>
> Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))
>
>
>
> On Mon, Jan 27, 2020 at 11:47 AM Peter Law  wrote:
>
>> Hi,
>>
>> Thanks for adding support for check constraints in Django 2.2, it's
>> great to be able to move constraints into the model definitions.
>>
>> I've been trying to work out how to express a constraint which
>> validates that the value of one field expresses a relation between two
>> other fields, but can't find a nice way to do so.
>>
>> I've read through the docs and also found
>> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
>> but haven't found a concise spelling.
>>
>> I've got a model like:
>>
>> ```
>> class Item(Model):
>> price = DecimalField()
>> full_price = DecimalField()
>> is_on_sale = BooleanField()
>> ```
>>
>> I'd like to be able to express neatly that the `is_on_sale` boolean be
>> true only when `price < full_price`.
>> In Postgres I can express this as:
>>
>> ```
>> ALTER TABLE item
>> ADD CONSTRAINT is_on_sale_check
>> CHECK (is_on_sale = (price < full_price))
>> ```
>>
>> However in Django I can't find a way to express this directly.
>>
>> I did find a long spelling which essentially checks the True case and
>> the False case explicitly and then ORs them together, however it's
>> several lines of `models.Q` combinations and not at all clear about
>> what the code is trying to achieve.
>>
>> Is there a concise way to do this sort of constraint? If not, would it
>> be possible for Django to add support for it?
>>
>> Thanks,
>> Peter
>>
>> --
>> 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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_yRT1F09KXXAJ9e_tdGzxjpy9TBf5B6%3D-Yyzp-Swepybay5w%40mail.gmail.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread DANIEL URBANO DE LA RUA
it is goin to be better and less cost for the db

El lun., 27 ene. 2020 a las 19:01, DANIEL URBANO DE LA RUA (<
dannybombas...@gmail.com>) escribió:

> take a look at this https://docs.djangoproject.com/en/3.0/topics/signals/
>
>
> El lun., 27 ene. 2020 a las 18:59, Stephen J. Butler (<
> stephen.but...@gmail.com>) escribió:
>
>> Frankly, if is_on_sale has such a tight constraint I wouldn't have it as
>> its own column. Why not just make it an annotated field with an F
>> expression?
>>
>>
>> https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations
>>
>> Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))
>>
>>
>>
>> On Mon, Jan 27, 2020 at 11:47 AM Peter Law  wrote:
>>
>>> Hi,
>>>
>>> Thanks for adding support for check constraints in Django 2.2, it's
>>> great to be able to move constraints into the model definitions.
>>>
>>> I've been trying to work out how to express a constraint which
>>> validates that the value of one field expresses a relation between two
>>> other fields, but can't find a nice way to do so.
>>>
>>> I've read through the docs and also found
>>> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
>>> but haven't found a concise spelling.
>>>
>>> I've got a model like:
>>>
>>> ```
>>> class Item(Model):
>>> price = DecimalField()
>>> full_price = DecimalField()
>>> is_on_sale = BooleanField()
>>> ```
>>>
>>> I'd like to be able to express neatly that the `is_on_sale` boolean be
>>> true only when `price < full_price`.
>>> In Postgres I can express this as:
>>>
>>> ```
>>> ALTER TABLE item
>>> ADD CONSTRAINT is_on_sale_check
>>> CHECK (is_on_sale = (price < full_price))
>>> ```
>>>
>>> However in Django I can't find a way to express this directly.
>>>
>>> I did find a long spelling which essentially checks the True case and
>>> the False case explicitly and then ORs them together, however it's
>>> several lines of `models.Q` combinations and not at all clear about
>>> what the code is trying to achieve.
>>>
>>> Is there a concise way to do this sort of constraint? If not, would it
>>> be possible for Django to add support for it?
>>>
>>> Thanks,
>>> Peter
>>>
>>> --
>>> 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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_yRT3RetnT%3De3UU80bSUaiofnzcT7%2Bqbz5COuThcSd0Zt-Bw%40mail.gmail.com.


Re: How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Stephen J. Butler
Frankly, if is_on_sale has such a tight constraint I wouldn't have it as
its own column. Why not just make it an annotated field with an F
expression?

https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations

Item.objects.annotate(is_on_sale=(F('price') < F('full_price')))



On Mon, Jan 27, 2020 at 11:47 AM Peter Law  wrote:

> Hi,
>
> Thanks for adding support for check constraints in Django 2.2, it's
> great to be able to move constraints into the model definitions.
>
> I've been trying to work out how to express a constraint which
> validates that the value of one field expresses a relation between two
> other fields, but can't find a nice way to do so.
>
> I've read through the docs and also found
> https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
> but haven't found a concise spelling.
>
> I've got a model like:
>
> ```
> class Item(Model):
> price = DecimalField()
> full_price = DecimalField()
> is_on_sale = BooleanField()
> ```
>
> I'd like to be able to express neatly that the `is_on_sale` boolean be
> true only when `price < full_price`.
> In Postgres I can express this as:
>
> ```
> ALTER TABLE item
> ADD CONSTRAINT is_on_sale_check
> CHECK (is_on_sale = (price < full_price))
> ```
>
> However in Django I can't find a way to express this directly.
>
> I did find a long spelling which essentially checks the True case and
> the False case explicitly and then ORs them together, however it's
> several lines of `models.Q` combinations and not at all clear about
> what the code is trying to achieve.
>
> Is there a concise way to do this sort of constraint? If not, would it
> be possible for Django to add support for it?
>
> Thanks,
> Peter
>
> --
> 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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxWtpqnDr_8WrQiskH7wXxBPyJ3sTWa%2BAsK1592SXWTfpw%40mail.gmail.com.


Django 3 material

2020-01-27 Thread Nathaniel Brown
I am looking for the best Django 3.0 books or learning material.

Any help would be greatly appreciated.

-- 
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/CAN_i64%2Bob3hLtwY4SQcTAHyEkDLAVkQZpYdG_48vC4uzesejgg%40mail.gmail.com.


How to express `foo = (bar < quox)` as a constraint

2020-01-27 Thread Peter Law
Hi,

Thanks for adding support for check constraints in Django 2.2, it's
great to be able to move constraints into the model definitions.

I've been trying to work out how to express a constraint which
validates that the value of one field expresses a relation between two
other fields, but can't find a nice way to do so.

I've read through the docs and also found
https://groups.google.com/d/topic/django-users/6Olh5V1b7Us/discussion,
but haven't found a concise spelling.

I've got a model like:

```
class Item(Model):
price = DecimalField()
full_price = DecimalField()
is_on_sale = BooleanField()
```

I'd like to be able to express neatly that the `is_on_sale` boolean be
true only when `price < full_price`.
In Postgres I can express this as:

```
ALTER TABLE item
ADD CONSTRAINT is_on_sale_check
CHECK (is_on_sale = (price < full_price))
```

However in Django I can't find a way to express this directly.

I did find a long spelling which essentially checks the True case and
the False case explicitly and then ORs them together, however it's
several lines of `models.Q` combinations and not at all clear about
what the code is trying to achieve.

Is there a concise way to do this sort of constraint? If not, would it
be possible for Django to add support for it?

Thanks,
Peter

-- 
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/CAEMtty7rsf2qk_0qXnzqZdA89e6VRCqF%2B4nQSHgxVCE4XfKAnw%40mail.gmail.com.


Cannot list the content of a subdirectory of static in my django projet

2020-01-27 Thread Guy NANA
I have in my project a directory named uts in static/ folder that I want to 
list the content and send it in HttpResponse but the glob.glob() function 
doesn't work.
Here you have the structure of my project. The utility function 
*list_dir_content* which call glob.glob is located in the utils folder: 
*utils/data.py*
import glob
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# def get_remote_dataset():

# def get_local_dataset():


def list_dir_content(dir_path, file_extension=''):
files_pattern = dir_path + '/*.' + file_extension
print("files_pattern : ", files_pattern)
#files_results_paths = glob.glob(files_pattern)
files_results_paths = 
glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.')
print("files_results_paths : ", files_results_paths)
return files_results_paths
## in models.py
from django.db import models
from django.conf import settings

# utils methods import
import utils.data as data
import os
from django.conf import settings

# Create your models here.
class Dataset(models.Model):
"""docstring for dataset"""
dataset_path = models.CharField(max_length=256)
dataset_name = models.CharField(max_length=256)
dataset_nb_instances = models.CharField(max_length=256)
dataset_type = models.CharField(max_length=3)
dataset_adding_date = models.DateField()

def __str__(self):
return "dataset_path : {0}  - dataset_name : {1} - number of instances : 
{2}".format(self.dataset_path, self.dataset_name, self.dataset_nb_instances)
@classmethod
def get_uts_datasets(cls):
#uts_datasets_path = os.path.join(settings.DATASETS_DIR, 'uts')
#print("pathname of uts datasets repositorie : ", uts_datasets_path)
uts_datasets_files = data.list_dir_content(settings.UTS_DATASETS_DIR)
print("UTS list of files : ", uts_datasets_files)
uts_datasets = []
for uts_datasets_file in uts_datasets_files:
dataset_type = 'uts'
dataset_path = uts_datasets_file
dataset_name = data.get_dataset_name(uts_datasets_file)
dataset_nb_instances = data.get_nb_instances(uts_datasets_file)
uts_dataset = Dataset(dataset_path = dataset_path, dataset_name = 
dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = 
dataset_type)
uts_datasets.append(uts_dataset)
print("UTS datasets : ", uts_datasets)

return uts_datasets

@classmethod 
def get_mts_datasets(cls):
mts_datasets_path = settings.DATASETS_DIR + '/mts'
mts_datasets_files = data.list_dir_content(mts_datasets_path)
mts_datasets = []
for mts_datasets_file in mts_datasets_files:
dataset_type = 'mts'
dataset_path = mts_datasets_file
dataset_name = data.get_dataset_name(mts_datasets_file)
dataset_nb_instances = data.get_nb_instances(mts_datasets_file)
mts_dataset = Dataset(dataset_path = dataset_path, dataset_name = 
dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = 
dataset_type)
mts_datasets.append(mts_dataset)

return mts_datasets

## in views.py
def server_uts_datasets(request):
if request.method == 'GET':
uts_datasets = Dataset.get_uts_datasets()
uts_datasets_serializer = DatasetSerializer(uts_datasets, many=True)
print(uts_datasets)
return JsonResponse(uts_datasets_serializer.data, safe=False)

Thanks for your kindly help in advance...

[image: gjangoproject_structure.JPG] 

-- 
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/ef243ed3-bf9c-4acd-a596-38b67c879230%40googlegroups.com.
import glob
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# def get_remote_dataset():

# def get_local_dataset():
	


def list_dir_content(dir_path, file_extension=''):
	files_pattern = dir_path + '/*.' + file_extension
	print("files_pattern : ", files_pattern)
	#files_results_paths = glob.glob(files_pattern)
	files_results_paths = glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.')
	print("files_results_paths : ", files_results_paths)
	return files_results_paths

def get_dataset_name(file_path):
	dataset_filename = os.path.split(file_path)[-1] # -> (head, tail); retrie e the last componnent, the tail, of the pathname path. Head is ignored.
	dataset_name = dataset_filename.split('.')[0] # get the name without the extension file
	return dataset_name

def get_nb_instances(file_path):
	return 200
#from django.shortcuts import render
from django.http import HttpResponse #, HttpResponseRedirect
#from django.template import RequestContext
#from django.urls import reverse

# DRF imports
from rest_framework import viewsets, status
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse
from django.views.decorators.csrf import csrf_exempt

# models imports
from .models import Dataset

# serializers import
from .serializers import DatasetSerializer

# import of python ML libraries
import 

Re: Hosting a django website

2020-01-27 Thread graeme
I have had issues with Django and cpanel. Last time I tried it was less 
than straightforward (cpanel's version of Apache would not work with the 
distro's mod_wsgi).

There are a number shared hosts who explicitly support Django, and even 
some who specialise in it. I would avoid deploying Django on others

A VPS is not that hard to set up and better value.


On Sunday, January 26, 2020 at 2:52:26 PM UTC, Laser Clinque wrote:
>
> You can use any modern cpanel or directadmin panel. I am using the 
> stablepoint and hosting many django sites. 
>
> On Saturday, 25 January 2020 23:42:13 UTC+5, Perceval Maturure wrote:
>>
>> dear all
>> 1.what is the best way to host a django website on a shared host that you 
>> are not sudo?
>> 2.is this possible?
>> 3. any ideas?
>>
>> -- 
>> *Perceval Maturure*
>>
>> *083 303 9423*
>>
>

-- 
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/750d24ae-a1db-461d-bf52-b3cb46e8335f%40googlegroups.com.


Re: Getting the first item in a dict

2020-01-27 Thread S D
I ended up using `dict[next(iter(dict))]`

I am getting the dict as one of two dicts nested in a list from a weather
service API. I do this using a list comprehension to call the API with
coordinates for each location in another list. A mouthful.

`resp = [fetch_api_data(location, request_id) for location in
REQUEST_LOCATIONS]`

I then cycle through the list and, for each dict, persist a WeatherRequest
instance to db like so:

`
for item in weather_data:
data = item[next(iter(item))]
WeatherRequest(
date=data['date'],
request_id=data['request_id'],
request_location=data['request_location'],
type_icon=data['type_icon'],
description=data['description'],
temperature=data['temperature'],
wind_speed=data['wind']['speed'],
wind_bearing=data['wind']['bearing'],
wind_gust=data['wind']['gust'],
rain_prob=data['rain_prob'],
latitude=data['latitude'],
longitude=data['longitude']
).save()
`

Thanks.


Kind regards,
- SD


On Mon, Jan 27, 2020 at 2:51 PM Bill Freeman  wrote:

> Note that these give the only value.  This won't work if you have more
> than one value in the dict, since you won't know which you will get.  Where
> d is the dict:
>
> list(d.values())[0]
>
> or
>
> for i in d.values():
> # use i here
> print(i)
>
> or
>
> d[list(d)[0]]
>
> I'm sure that there are other ways.  There is certainly at least a way to
> play with the iterator protocol without using "for", but it may be harder
> to read.  You could put break at the end of the loop above to make it more
> apparent that it only runs once.
>
> On Mon, Jan 27, 2020 at 6:58 AM S D  wrote:
>
>> I have a dictionary which contains one item (“current_location”, which is
>> a nested dict) and I would like to access that nested dict. However, I
>> cannot use the key as the code will break if a different key is passed,
>> e.g. “different_location”.
>>
>> How can I access the first item in a dictionary without using a key? The
>> dict looks like this:
>>
>> `
>> {'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon':
>> 'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28,
>> 'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02,
>> 'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364,
>> 'request_location': 'Current location'}}
>> `
>>
>> Kind regards,
>> - SD
>>
>> --
>> 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/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAB%2BAj0tFp_qOtqd3WMvPTfGkGy3q-jCFDwnrgheYjBk8NqP4bQ%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAH-SnCBkMAJqVetRU_X46r-pg-HLDZsuTt4iqeYN%2BSpjSmbttA%40mail.gmail.com.


Re: Getting the first item in a dict

2020-01-27 Thread Bill Freeman
Note that these give the only value.  This won't work if you have more than
one value in the dict, since you won't know which you will get.  Where d is
the dict:

list(d.values())[0]

or

for i in d.values():
# use i here
print(i)

or

d[list(d)[0]]

I'm sure that there are other ways.  There is certainly at least a way to
play with the iterator protocol without using "for", but it may be harder
to read.  You could put break at the end of the loop above to make it more
apparent that it only runs once.

On Mon, Jan 27, 2020 at 6:58 AM S D  wrote:

> I have a dictionary which contains one item (“current_location”, which is
> a nested dict) and I would like to access that nested dict. However, I
> cannot use the key as the code will break if a different key is passed,
> e.g. “different_location”.
>
> How can I access the first item in a dictionary without using a key? The
> dict looks like this:
>
> `
> {'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon':
> 'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28,
> 'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02,
> 'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364,
> 'request_location': 'Current location'}}
> `
>
> Kind regards,
> - SD
>
> --
> 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/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB%2BAj0tFp_qOtqd3WMvPTfGkGy3q-jCFDwnrgheYjBk8NqP4bQ%40mail.gmail.com.


Re: Getting the first item in a dict

2020-01-27 Thread Nick Sarbicki
I think the real question here is why you have a dict with one item in it?

If you can construct it so that this isn't the case that would be ideal.

If you really can't construct it any other way then you can do something
simple like my_dict.popitem()[1] to get the value of the only item in the
dict. Although it is worth noting this will also remove that item from the
dict (giving you a single non-nested dict which seems more desirable).


- Nick


On Mon, Jan 27, 2020 at 12:32 PM Mike Dewhirst 
wrote:

> Can you use ddict[ddict.keys()[0]] ???
>
> I don't know if that would show you the first item unless it was an
> ordered dict.
>
> Mike
>
>
>
>
>
>  Original message 
> From: S D 
> Date: 27/1/20 22:56 (GMT+10:00)
> To: django-users@googlegroups.com
> Subject: Getting the first item in a dict
>
> I have a dictionary which contains one item (“current_location”, which is
> a nested dict) and I would like to access that nested dict. However, I
> cannot use the key as the code will break if a different key is passed,
> e.g. “different_location”.
>
> How can I access the first item in a dictionary without using a key? The
> dict looks like this:
>
> `
> {'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon':
> 'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28,
> 'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02,
> 'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364,
> 'request_location': 'Current location'}}
> `
>
> Kind regards,
> - SD
>
> --
> 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/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5e2ed855.1c69fb81.32672.ad44SMTPIN_ADDED_MISSING%40gmr-mx.google.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGuvt90w7nW4cGBRjX%2BDtA5%2BvHk9i17rnfwjmF8o1XOZJ_QJyA%40mail.gmail.com.


RE: Getting the first item in a dict

2020-01-27 Thread Mike Dewhirst
Can you use ddict[ddict.keys()[0]] ???I don't know if that would show you the 
first item unless it was an ordered dict. Mike
 Original message From: S D  Date: 27/1/20  
22:56  (GMT+10:00) To: django-users@googlegroups.com Subject: Getting the first 
item in a dict I have a dictionary which contains one item (“current_location”, 
which is a nested dict) and I would like to access that nested dict. However, I 
cannot use the key as the code will break if a different key is passed, e.g. 
“different_location”.How can I access the first item in a dictionary without 
using a key? The dict looks like this:`{'current_location': {'date': 
'2020-01-27T10:28:24.148Z', 'type_icon': 'partly-cloudy-day', 'description': 
'Mostly Cloudy', 'temperature': 68.28, 'wind': {'speed': 10.48, 'bearing': 178, 
'gust': 12.47}, 'rain_prob': 0.02, 'latitude': '-33.927407', 'longitude': 
'18.415747', 'request_id': 31364, 'request_location': 'Current location'}}`Kind 
regards,- SD



-- 
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/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5e2ed855.1c69fb81.32672.ad44SMTPIN_ADDED_MISSING%40gmr-mx.google.com.


Getting the first item in a dict

2020-01-27 Thread S D
I have a dictionary which contains one item (“current_location”, which is a
nested dict) and I would like to access that nested dict. However, I cannot
use the key as the code will break if a different key is passed, e.g.
“different_location”.

How can I access the first item in a dictionary without using a key? The
dict looks like this:

`
{'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon':
'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28,
'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02,
'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364,
'request_location': 'Current location'}}
`

Kind regards,
- SD

-- 
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/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%40mail.gmail.com.