Re: annotate sum many record

2019-06-02 Thread Aditya Singh
Hello mate,
Simple get the value from the objects and store in separate variables. Then
you may do any and as many calculations you want and return the desired
value :)
Kind Regards,
Aditya

On Mon, Jun 3, 2019, 12:18 PM carlos  wrote:

> Hi, i used django 1.11 and try used annotate
>
> i have model with 3 decimal fields i would like sum 2 and multiply for
> other field but with aritmetic calculation
> i have used this query
>
> (1) total = Model.objects.annotate(all=Sum(F('fieldA') + F('fieldB') *
> F('fieldC')))
> but this return queryset with all calculation i have 3 record in db
> print total
> and return this
>
> , ,  object>]>
>
> then I do a forloop with for to do the sum
> for x in total:
> variable += x.all
>
> it is possible to return the sum of the 3 registers with the annotate in a
> single line,
> as (1)total queryset
>
> How could I do that?
>
> thank for yours helps
>
>
> --
> att.
> Carlos Rocha
>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAM-7rO1LGRSMoBMC435nf93P-__oo%3DhYz_hwBiW6WxVeip8uvw%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEPfumi6LzOigpLiSmWuuyC7Wg7_U5_haSLYCksJcO0wbr5D6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


annotate sum many record

2019-06-02 Thread carlos
Hi, i used django 1.11 and try used annotate

i have model with 3 decimal fields i would like sum 2 and multiply for
other field but with aritmetic calculation
i have used this query

(1) total = Model.objects.annotate(all=Sum(F('fieldA') + F('fieldB') *
F('fieldC')))
but this return queryset with all calculation i have 3 record in db
print total
and return this

, , ]>

then I do a forloop with for to do the sum
for x in total:
variable += x.all

it is possible to return the sum of the 3 registers with the annotate in a
single line,
as (1)total queryset

How could I do that?

thank for yours helps


-- 
att.
Carlos Rocha

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAM-7rO1LGRSMoBMC435nf93P-__oo%3DhYz_hwBiW6WxVeip8uvw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: TemplateSyntaxError

2019-06-02 Thread anchal agarwal
You were rightthe error resolved when I removed curly braces from the
if statement. Thanks alot !!
On 01-Jun-2019 11:26 am, "sagar ninave"  wrote:

> you do not have ended for loop
>
> On Fri, May 31, 2019 at 7:41 PM anchal agarwal 
> wrote:
>
>> I am creating a simple form in which when a user will select a particular
>> song and press Favorite button a star image will get pop up in front of
>> that song.
>> But it is showing TemplateSyntaxError.
>> Please correct me where I am wrong
>> here is my template
>> 
>>
>> {{ album.album_title}}
>> {{ album.artist }}
>>
>> {% if error_message %}
>> {{ error_message }}
>> {% endif %}
>>
>> 
>> {% csrf_token %}
>> {% for song in album.song_set.all %}
>> 
>> 
>> {{ song.song_title }}
>> {{% if song.is_favorite %}}
>> http://i.imgur.com/b9b13Rd.png";>
>> {{% endif %}}
>> 
>> 
>> 
>>
>> --
>> 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 post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/
>> msgid/django-users/CAMT%3DisUed7dkD%2BPCgis7bBZSAveti1Wy4WgNSoPA7D
>> m6kKLO3A%40mail.gmail.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
>
> 
> sagar ninave
> about.me/sagarninave
> 
>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/CAA6pdZ-yw6hsS7aOj4czvEY5uCeb5NdYOuUiY
> 50SvGvhyPA5_g%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMT%3DisXQdR2fvYbE-vGhOZVMrnqWvpXR5Rb%3D3XXF6a5Fi8RiqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Atomic IntegerField increment based on another model instance

2019-06-02 Thread Ryan Causey
The solution I ended up going with was using a PostgreSQL table lock:
@contextmanager
@transaction.atomic
def lock_table(model):
"""Performs a table lock on the provided model.

Note that this locks the entire table for the given model
and prevents any other operations from occurring on that
model. This should be used sparingly and not be used on
long running processes if possible.

This only works on PostgreSQL databases.
"""
with connection.cursor() as cursor:
# We use the most restrictive lock mode, ACCESS EXCLUSIVE, to prevent any
# other operations from being performed on the table while we hold this 
lock.
# See: https://www.postgresql.org/docs/current/explicit-locking.html
cursor.execute(f"LOCK TABLE {model._meta.db_table} IN ACCESS EXCLUSIVE MODE"
)
yield

I found, perhaps accidentally a way of doing it purely in the ORM but it 
seemed gross, was slower, and probably was a happy accident rather than a 
real solution. For the morbidly curious, it took making account_number a 
nullable field and updating the account number after save like so:

# My best guess is this locks the entire table? Without this it doesn't 
work.
list(Account.objects.select_for_update().all())
# Update ourself
Account.objects.filter(
pk = self.pk,
).annotate(
other_accounts_exist = models.Exists(
Account.objects.filter(
jurisdiction = models.OuterRef("jurisdiction"),
account_number__isnull = False,
).exclude(
pk = models.OuterRef("pk"),
),
),
).update(
account_number = models.Case(
models.When(
other_accounts_exist = True,
then = models.Subquery(
Account.objects.filter(
jurisdiction = models.OuterRef("jurisdiction"),
account_number__isnull = False,
).exclude(
pk = models.OuterRef("pk"),
).order_by(
"-account_number",
).values(
"account_number",
)[:1]
) + models.Value(1),
),
default = 1,
)
)
# Since we did an update we need to refresh ourselves from the db.
self.refresh_from_db()


On Saturday, June 1, 2019 at 8:44:29 PM UTC-7, Ryan Causey wrote:
>
> Assume the following models for this question:
>
> class Jurisdiction(models.Model):
> name = models.CharField(max_length = 100)
>
> class Account(models.Model):
> account_number = models.PositiveIntegerField(blank = True)
> jurisdiction = models.ForeignKey("Jurisdiction", on_delete = models.
> CASCADE)
>
> class Meta:
> """Meta model to define unique_together constraints."""
> #make the account_number unique for a jurisdiction
> unique_together = ("account_number", "jurisdiction")
>
> def create_account_number(self):
> """Create the account number for this account."""
> # If the account number is already set, do nothing.
> if self.account_number is not None:
> return
>
> # Get the last account created for the jurisdiction, based on 
> having the largest account number.
> last_account_for_jurisdiction = Account.objects.filter(jurisdiction 
> = self.jurisdiction).order_by("-account_number").first()
> # If the account exists set the account number.
> if last_account_for_jurisdiction is not None:
> self.account_number = 
> last_account_for_jurisdiction.account_number 
> + 1
> # Else there are no other accounts for this jurisdiction, set the 
> first account number.
> else:
> self.account_number = 1
>
> def save(self, *args, **kwargs):
> # create the account number
> self.create_account_number()
> # call the superclass save to write the account to the database.
> super().save(*args, **kwargs)
>
> Is there a way in Django to be able to atomically set account_number on 
> save based on the largest account number for a given jurisdiction? I was 
> hoping some combination of F expressions 
> ,
>  
> conditional expressions 
> , 
> and subquery expressions 
>  would 
> allow me to do this, but they don't appear to allow referencing a different 
> model's fields.
>
> The unique_together will properly prevent duplicate Jurisdiction + 
> account_number pairs from being entered into the DB, but I was hoping there 
> was a way to perform the setting of account_number in an atomic way so I 
> wouldn't have to handle an IntegrityError and try again. I get the feeling 
> what I'm asking for isn't possible, or there's a completely different way I 
> should be going about this. I haven't used AutoField as I need a sequental 
> series of numbers within each Jurisdiction.
>
> Thanks in advanc

Trying to add radio button and store its value in database.. Django

2019-06-02 Thread abb
https://stackoverflow.com/questions/56407153/radio-button-with-default-selection-in-django
 

Not understanding where am i going wrong here.

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b54cb2eb-1de6-4619-aab6-5957a12ef4c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.