Wednesday, March 29, 2017

Re: TransactionManagementError from inside atomic block

Hey Simon,

Thank you for your response, we were able to resolve the problem soon after I posted the query.

And yes, you are correct, it was an issue with having multiple DB connections and the `auto-commit` being turned off for one, but then another connection being used for querying.

We earlier had a master-slave configuration for our DB and were using a DB router to route all reads to the slave. That is still in use and that is why the `deafualt` connection was used as we entered the `atomic` block, but `slave` when we made the read.

Thank you again.

On Thu, Mar 30, 2017 at 4:39 AM Simon Charette <charette.s@gmail.com> wrote:
Hi Ketan,

I'm afraid this will be really hard to solve without the code that is raising this exception.

Some details that could help debug the issue:

1. Are your workers multi-threaded?
2. Are you sure you are not calling functions performing manual transaction management?
3. Is your atomic() block using the same database involved in the select_for_update() call?

e.g. Are you using transaction.atomic() (which will use the default database)
but have database routers that could route reads/writes to another db?

Cheers,
Simon

Le samedi 25 mars 2017 15:08:19 UTC-4, Ketan Bhatt a écrit :
I have a method that updates a row in the table.
To avoid race condition, I locked the row using `select_for_update` inside an `atomic` block and doing the update.
This method is called from a celery task.

This works well on my local machine. But when this gets called on my production server (two tasks being picked up by two workers at the same time and therefore trying to access the same row at once), I get: 
`TransactionManagementError('select_for_update cannot be used outside of a transaction.',)`

I checked that to use `select_for_update`, autocommit should be False.
Inside the atomic block `get_autocommit` returns False.

Now on production this must be returning `TRUE`.


What could be the reason?

--
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 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/91f6b088-e255-4ff9-a9c5-f033ffdf4211%40googlegroups.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+unsubscribe@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/CACyRkBVpKO1Khuf4Z%3D_PggA3TPcFa92anCgC_p_tzw92kwbQVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: TransactionManagementError from inside atomic block

Hi Ketan,

I'm afraid this will be really hard to solve without the code that is raising this exception.

Some details that could help debug the issue:

1. Are your workers multi-threaded?
2. Are you sure you are not calling functions performing manual transaction management?
3. Is your atomic() block using the same database involved in the select_for_update() call?

e.g. Are you using transaction.atomic() (which will use the default database)
but have database routers that could route reads/writes to another db?

Cheers,
Simon

Le samedi 25 mars 2017 15:08:19 UTC-4, Ketan Bhatt a écrit :
I have a method that updates a row in the table.
To avoid race condition, I locked the row using `select_for_update` inside an `atomic` block and doing the update.
This method is called from a celery task.

This works well on my local machine. But when this gets called on my production server (two tasks being picked up by two workers at the same time and therefore trying to access the same row at once), I get: 
`TransactionManagementError('select_for_update cannot be used outside of a transaction.',)`

I checked that to use `select_for_update`, autocommit should be False.
Inside the atomic block `get_autocommit` returns False.

Now on production this must be returning `TRUE`.


What could be the reason?

--
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 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/91f6b088-e255-4ff9-a9c5-f033ffdf4211%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Executing DB-Queries during import

> Is it advisable to execute db queries in my AppConfig ready signal handler?

AppConfig.ready() is still at module-level.

> Use case: I want to add URL patterns which get collected from db rows.

It might be more appropriate to define an URL resolver (an object like django.conf.url)
that performs queries at resolve()/reverse() time instead.

A simpler solution could be to define a catch all view like django-cms does and
perform the routing inside the view.

Cheers,
Simon

Le mercredi 29 mars 2017 08:39:52 UTC-4, guettli a écrit :


Am Mittwoch, 22. März 2017 19:20:43 UTC+1 schrieb Tim Graham:
Correct, module level queries should be avoided. Django might try to add some detection to warn about or prohibit that, see https://groups.google.com/d/topic/django-developers/7JwWatLfP44/discussion.


I understand db queries at module-level (aka "import time") should not happen.

When I am allowed to do db queries.

Is it advisable to execute db queries in my AppConfig ready signal handler?

Use case: I want to add URL patterns which get collected from db rows.


--
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 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/1687811b-b779-4031-a127-d9549bc0bdf3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: From SQL to Django Queryset API

Hello Mauro,

The following should do

from django.db.models import F

Movimento.objects.values(
    'anno','impianto'
).annotate(
    dare=Sum('dare),
    avere=Sum('avere),
    diff=Sum(F('dare') - F('avere)),
)

Cheers,
Simon

Le mercredi 29 mars 2017 16:19:37 UTC-4, Mauro Ziliani a écrit :
Hi all.
My name's Mauro and I working on a DB (SQLite3 now and Postgres in the future).

I have a table Movimento with the fields
anno integer,impianto integer,dare decimal(10,2),avere decimal(10,2)

BY hands I can run

SELECT anno,impianto, sum(dare), sum(avere) FROM movimento 
GROUP BY anno,impianto
ORDER BY anno,impianto 

With django Api I write

Movimento.objects.values('anno','impianto').annotate(dare=Sum('dare), avere=Sum('avere))

And I get the same beaviour.

Now I need to translate the following SQL script

SELECT anno,impianto, sum(dare), sum(avere), sum(dare-avere)
FROM movimento 
GROUP BY anno,impianto
ORDER BY anno,impianto 

into django API queryset.

Is it possible todo this in one row?

Cna you give me some idea to solve this translaion?

Finally I'll render the results into a table.

Best regards,
   MZ

--
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 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/9c8f43e7-6d84-4190-9dbc-beb25b3e1a95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: NoReverseMatch when trying to use get_absolute_url with custom template tag

On Wednesday 29 March 2017 09:16:17 Will Holmes wrote:

 

> url(r'^(?P<id>\d+)$/', views.viewEvent, name='viewevent'),

 

name = viewevent

 

> def get_absolute_url(self):

> return reverse('cal:viewEvent', args=[str(self.id)],

> current_app='cal')

 

name = viewEvent

 

> Sorry about the load of code posted. Any chance anyone knows what

> might have caused this issue?

 

Do you see it now?

--

Melvyn Sopacua

NoReverseMatch when trying to use get_absolute_url with custom template tag

I am making a calendar app which has a custom template tag that takes the python HTML template function and overlays objects from the Events model on the correct days. I am trying to get the displayed objects to have a link directly to an object detail/edit view and am trying to use get_absolute_url and to reverse render this view. This is necessary as the custom template tag doesn't load correctly if I try to hardcode {% url %} template tags into it for each event via a for loop. I have spent some hours looking through stack overflow questions with no luck and have even changed my reverse to the object ID rather than the title of the event. I am hoping this is just a small thing that I have overlooked but no sure.


view:

def home(request, month=None, year=None):      if month == None:          _date = datetime.now()      else:          _date = date(int(year), int(month), 1)      title = "%s, %s" % (_date.strftime("%B"), _date.strftime("%Y"))        return render(request, 'calendar.html', calendar(_date, title))


url:

app_name = 'cal'  urlpatterns = [      url(r'^$', views.home, name='home'),      url(r'^newevent/$', views.newEvent, name='newevent'),      url(r'^(?P<id>\d+)$/', views.viewEvent, name='viewevent'),      url(r'^(?P<month>\d+)/(?P<year>\d+)$', views.home, name='another-month')  ]


HTML:

    <div>        {% load calendarify %}        <span id="calendarify">{% calendarify year month event_list %}</span>      </div>


Template tag relevant function:

    def formatday(self, day, weekday):          if day != 0:              cssid = self.cssclasses[weekday]              cssclass = "daybox"              if date.today() == date(self.year, self.month, day):                  cssid += ' today'              if day in self.events:                  cssid += ' filled'                  body = ['<ul>']                  for event in self.events[day]:                      body.append('<li>')                      body.append('<a href="%s">' % event.get_absolute_url())                      body.append(esc(event.title))                      body.append('</a></li>')                  body.append('</ul>')                  return self.day_cell(                      cssclass, cssid, '<span class="dayNumber">%d</span> %s' % (                          day, ''.join(body)))              return self.day_cell(                  cssclass, cssid, '<span class="dayNumberNoReadings">%d</span>' % (day))          return self.day_cell('nodaybox', 'noday', '&nbsp;')


Model:

class Events(models.Model):      ...        def get_absolute_url(self):          return reverse('cal:viewEvent', args=[str(self.id)], current_app='cal')


Sorry about the load of code posted. Any chance anyone knows what might have caused this issue?

--
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 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/aae357a0-84a7-46bd-a93d-4aac61f352c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Meta class completely missing.

Done. No change.

What is also very bizarre is that *not* having a Meta class would make the "Restaurant" test (tests/model_inheritance/models.py : Line 107) break badly; class Restaurant has a Meta that subclasses the Meta of class Rating. So if Meta does not exist... well you see the problem. In fact this *exact* thing occurs in my situation. 

I have a "class Case(models.Model)" with a "class Meta:" and a "class MDxCase(Case):" if I try to do "class Meta(Case.Meta):" for class MDxCase an AttributeError error is thrown "Case has no attribute 'Meta'". 

This is very very odd.
Can anyone explain this behavior or have any other ideas to try?

--Brandon

On Mon, Mar 20, 2017 at 4:55 AM, chris rose <chris.rose.one@gmail.com> wrote:
hi brandon

try removing (object) from your meta class declaration

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/gGfLmihCilI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@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/30d054d7-8150-4eeb-bc3b-cf8a762078ae%40googlegroups.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+unsubscribe@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/CAFfc3713xmobr7-VH%2BdTDyGdN2fAyw_KgmmRmw1GZovcTA%3DgpA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.