Re: url - hot to set properly?

2011-03-27 Thread Pascal Germroth

Hi,


urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^', include('apps.textcontent.urls')),
)



when i pass: /admin/ all is ok, but when I pass /admin -
apps.textcontent.urls are executed, but why?


Well because the URL pattern is a regular expression that is matched 
against the URL, and you explicitly stated that there has to be a / at 
the end.


Change it to url(r'^admin/?',…) and it will work both with '/admin' and 
'/admin/'.


But: this is bad URL etiquette. You should choose a schema (/ or not) 
and stick with it. Even better: create a view that matches the opposite 
of your chosen schema and permanently redirects to the correct URL (or 
enable normalisation in your webserver, that would be easier)


--
Pascal Germroth

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Query Builder & temporary tables

2011-03-02 Thread Pascal Germroth
Hi,

using the PostgreSQL backend I have a complex query
  q = Model.objects.filter(…)
that selects some objects using multiple joins and some geoDjango magic.

Then there are some other simpler queries like
  q2 = OtherModel.filter(foreign__in = q.query)
  q3 = YetAnotherModel.filter(foreign__in = q.query)

I don't actually evaluate q but it's only input for the others. Now
evaluating q2 and q3 of course leads to basically executing q two times,
as a subquery.

So I would like to do a `CREATE TEMPORARY TABLE temp_q AS ` and use this table for q2 and q3, something like:

  new_q = Model.objects.raw('SELECT * FROM temp_q')
  q2 = OtherModel.filter(foreign__in = new_q.query)
  q3 = YetAnotherModel.filter(foreign__in = new_q.query)

My question: what is the nice way to do this? I would fall back to
string manipulation 'CREATE…AS ' + str(q.query) and using a cursor to
run this, but this feels hacky.

Of course q might still return a lot of objects, so creating a list of
primary keys in python and passing that as a parameter is no good either.



Cheers,
-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Query Builder forgets annotations

2011-02-27 Thread Pascal Germroth
Hi,

>> It seems the query builder forgets annotations when dealing with joins:
> Not in my experience.

Yeah I experimented further and it wasn't actually a problem with /that/
part of the query (well, it changed when I added/removed the order_by,
but the actual cause was some interference once it was evaluated). I'll
try to create a minimal example.

-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: foreign key reference in the 'def' to calculate tax

2011-02-26 Thread Pascal Germroth
Hi,

> class TaxReference(models.Model):
> tax_multiplier = models.DecimalField()

> class Product(models.Model):
> price = models.DecimalField(max_digits=10, decimal_places=2)
> tax = models.ForeignKey(TaxReference)
> def taxedprice(self):
> return self.price*TaxReference.tax_multiplier
> 
> The very last line of code is what I'm trying to accomplish.

Is this what you want?

def taxedprice(self):
  return self.price * self.tax.tax_multiplier


Cheers,

-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Query Builder forgets annotations

2011-02-26 Thread Pascal Germroth
Hi,

It seems the query builder forgets annotations when dealing with joins:


I have this model with Postgresql, Django 1.3 alpha 1 (SVN rev. 15659):

  class Corporation(Model):
uuid = UuidField(primary_key=True)

  class Branch(Model):
uuid = UuidField(primary_key=True)
corporation = ForeignKey(Corporation, related_name='branches')

Now I want to retrieve the corporations ordered by the number of their
branches:
  Corporation.objects.annotate(x=Count('branches')).order_by('x')

Which fails with:
  FieldError: Cannot resolve keyword 'x' into field. Choices are:
branches, uuid in /django/db/models/sql/query.py in setup_joins, line 1236

This
  Corporation.objects.annotate(x=Count('branches'))
yields
  SELECT corporation.uuid, COUNT(branch.uuid) AS x FROM corporation LEFT
OUTER JOIN branch ON (corporation.uuid = branch.corporation_id) GROUP BY
corporation.uuid, corporation.uuid

and executing this query manually, with `ORDER BY x` added, gives me
what I want.

Is this a django bug, or by design?

Now until this is fixed, how can I add this manually to the generated
statement? extra(order_by=['x']) also checks fields. But I don't want to
have to resort to completely raw queries…



Cheers,

-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Class-based views & authentication

2011-02-09 Thread Pascal Germroth
Hi,

>> To make things a bit easier, I'm about to write my own mixin for that so
>> I only have to provide a method that checks if credentials are OK.
> 
> As you've noticed, you can override the dispatch to decorate the view
> as required, and if you have a common authentication pattern, you can
> put that logic into a mixin.


For future reference, this is what I use now:


class LoginMixin(object):
  def get_test_func(self):
return getattr(self, 'test_func', lambda u: u.is_authenticated())
  def get_login_url(self):
return getattr(self, 'login_url', None)
  def get_redirect_field_name(self):
return getattr(self, 'redirect_field_name', None)
  def dispatch(self, request, *args, **kwargs):
from django.contrib.auth.decorators import user_passes_test
return user_passes_test(
  self.get_test_func(),
  login_url = self.get_login_url(),
  redirect_field_name = self.get_redirect_field_name()
)(super(LoginMixin, self).dispatch
)(request, *args, **kwargs)


class DashboardView(LoginMixin, TemplateView):
  login_url = '/base/login'
  template_name = 'dashboard.html'



LoginMixin *must* be the first base class, otherwise it could not
override View.dispatch in the other base classes.


-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Class-based views & authentication

2011-02-08 Thread Pascal Germroth
Hi,

I'm new to Django, but since this project will take a while I'm already
using 1.3 alpha since it will probably be released when I'm done…

As I understand it, the preferred method now are class-based views. But
I seem to be missing some kind of AuthenticationMixin… right now, have
to override `dispatch`, add the authentication decorator as one would
for function views, and call super.

To make things a bit easier, I'm about to write my own mixin for that so
I only have to provide a method that checks if credentials are OK.


Or am I doing this completely wrong?

-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Error when editing Inline OneToOneField

2011-02-07 Thread Pascal Germroth
Hi,

using SVN revision 15440 I get some weird behaviour:


Part of my model looks like this:

-
from django.contrib.gis.db import models

class Address(models.Model):
  uuid = UuidField(primary_key=True)
  readable = models.CharField(max_length=200)
  geo = models.GeometryField()

  def save(self, *args, **kwargs):
self.readable, self.geo = geocode(self.readable)
super(Address, self).save(*args, **kwargs)

class Corporation(models.Model):
  uuid = UuidField(primary_key=True)
  name = models.CharField(max_length=200)

class Branch(models.Model):
  uuid = UuidField(primary_key=True)
  address = models.OneToOneField(Address)
  corporation = models.ForeignKey(Corporation)
-

In the admin interface I use

-
class AddressAdmin(admin.ModelAdmin):
  readonly_fields = ('geo',)
admin.site.register(Address, AddressAdmin)

class BranchInline(admin.StackedInline):
  model = Branch
  fields = ('address',)

class CorporationAdmin(admin.ModelAdmin):
  inlines = [BranchInline]
  fields = ('name',)

admin.site.register(Corporation, CorporationAdmin)


Now for the weird part:

Add a corporation via admin interface, click save, works as expected.
Open the corporation again, in the inlined branch, add a new address,
click save, works as expected.

Then: open that corporation a third time, no matter what you do (delete
a branch, add another, change anything) clicking save returns this error:

MultiValueDictKeyError at
/admin/base/corporation/2b97beda-6893-426c-8229-ddf001394d36/
"Key 'branches-0-uuid' not found in "


I'm worried about the mention of branches.uuid -- this shows up
nowherein the generated form (only a select 'branches-0-address' with
the uuids of available addresses).

Playing around with fk_name on the inline admin brought me nothing but
error messages…

This only seems to concern the InlineModelAdmin. When I use seperate
ModelAdmin-s, everything works.


Where should I look for the bug? Might it be my custom UUID-field? (but
it seems to work…)



Cheers,

-- 
Pascal Germroth

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.