Re: how to set up a calculated field in Django?

2019-10-23 Thread Tyler Lynch
If you're using the newest postgres 12, you could also use their new 
generated column feature (
https://www.postgresql.org/docs/current/ddl-generated-columns.html). You 
can implement that in Django by editing the SQL in the migration setting up 
your table. Though I do look forward to this feature being integrated more 
directly into Django as a postgres-specific option, prly won't be too long 
until it's incorporated. 

On Monday, 21 October 2019 14:05:04 UTC-4, Eileen Bauer wrote:
>
> Hi,
> i have the following items in my model:
> mother_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
> null=True, default=1)
> father_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
> null=True, default=1)
>
> and I'd like to set up a generated field for them so I'd be able to detect 
> whether the child is an orphan or not. In MySQL i believe it'd look like 
> this:
> orphan varchar(101) GENERATED ALWAYS AS (mother_alive+father_alive) 
> VIRTUAL,
>
> I don't know how to change my model to do that...
>
> Any help?
>
> -Eileen
>
>

-- 
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/68c3b9cb-b3ad-4f83-a834-74051441a0d8%40googlegroups.com.


Re: how to set up a calculated field in Django?

2019-10-22 Thread wd
hi,

Besides the solution provided by @nm,  maybe you can do it by using
database trigger ...

On Tue, Oct 22, 2019 at 2:05 AM Eileen Bauer  wrote:

> Hi,
> i have the following items in my model:
> mother_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True,
> null=True, default=1)
> father_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True,
> null=True, default=1)
>
> and I'd like to set up a generated field for them so I'd be able to detect
> whether the child is an orphan or not. In MySQL i believe it'd look like
> this:
> orphan varchar(101) GENERATED ALWAYS AS (mother_alive+father_alive)
> VIRTUAL,
>
> I don't know how to change my model to do that...
>
> Any help?
>
> -Eileen
>
> --
> 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/32b1c56f-f52d-4b63-bd93-db25cc1e89a6%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/CABexzmiDjxRCBJR%2B-0o3OavfFMASL%2BtSpSWmi66nxbrp8nz9uA%40mail.gmail.com.


Re: how to set up a calculated field in Django?

2019-10-22 Thread nm
One way I can think of is to add a property 
 to your model. 
I believe something like this should work:

```
## in your model class:
mother_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
null=True, default=1)
father_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
null=True, default=1)

@property
def is_orphan(self):
return self.mother_alive == 0 and self.father_alive == 0  # not 
sure what the "no" value is, you have to set it appropriately
```
You'll then be able to access it as an attribute of the model:
```
Child.objects.get(id=1).is_orphan
# True or False
```

This value will not be stored in the database, though, and you won't be 
able to filter children by the `is_orphan` column, because there'll be no 
such column.

Another approach would be to use annotations 
.
 
With annotations, you can do aggregations, just like with any other model 
field.


On Monday, 21 October 2019 20:05:04 UTC+2, Eileen Bauer wrote:
>
> Hi,
> i have the following items in my model:
> mother_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
> null=True, default=1)
> father_alive = models.IntegerField(choices=YES_NO_CHOICES, blank=True, 
> null=True, default=1)
>
> and I'd like to set up a generated field for them so I'd be able to detect 
> whether the child is an orphan or not. In MySQL i believe it'd look like 
> this:
> orphan varchar(101) GENERATED ALWAYS AS (mother_alive+father_alive) 
> VIRTUAL,
>
> I don't know how to change my model to do that...
>
> Any help?
>
> -Eileen
>
>

-- 
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/cd4aed5d-223b-4c9a-9678-aa0fd5b9464f%40googlegroups.com.