Re: django user authenication form in modal form twitter bootstrap

2014-06-27 Thread Roman Klesel
I think the view function is wrong.

This should do:

from django.views.decorators.csrf import csrf_protect
from django.shortcuts impor render

@csrf_protect
def login(request):
return render(request, 'login.html')


2014-06-27 6:03 GMT+02:00 sarfaraz ahmed :
> Hello
>
> I am new to django I am trying to use modal (bootstrap) for embed by login
> form. I am using the following error. I have mentioned my code below and
> tried lot of googling... still no use.
>
> Help
>
> Reason given for failure:
>
> CSRF token missing or incorrect.
>
>
>
>
> Here is code in view.py
>
> from django.shortcuts import render_to_response
> from django.http import HttpResponseRedirect
> from django.contrib import auth
> from django.core.context_processors import csrf
> from django.template import RequestContext
> from django.views.decorators.csrf import csrf_protect
> from django.shortcuts import render
>
>
>
> @csrf_protect
> def login(request):
> c={}
> c.update(csrf(request))
> return
> render_to_response('login.html',c,context_instance=RequestContext(request))
>
>
>
> I call the login.html via link in nav bar using
>
>   data-target="#myModal">Login
>
> That is code for login.html which is modal included in my base.html
>
>
>  aria-labelledby="myModalLabel" aria-hidden="true">
> 
> 
> 
>  data-dismiss="modal"> class="sr-only">Close
> Modal title
> 
> 
> {% csrf_token
> %}
> User Name:
>  id="username">
> Password:
>  id="password">
> 
> 
> 
> 
>  data-dismiss="modal">Close
> Save
> changes
> 
> 
> 
> 
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/943e0bf4-ce4d-4ef5-ae45-57e86c36f367%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DKwRHU-G_jF4winxka2nnLRB%3D-qbejfDdnQ0GE0zNNMeQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Unable to add CSRF token to the header

2014-06-26 Thread Roman Klesel
Hello,

I'm not quite sure what you are trying to do, but with type: "GET"
there is no need to worry about crfs_token.

>From the link you posted:

"""
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));

"""

For the thing with the unexpected login ... I also think this has
nothing to do with the crfs_token thing. If you have the default
authentication running, your authentication data will be in a cookie
named session-id or so ...
of course if you mess up that cookie, you have to authenticate again.


2014-06-23 4:55 GMT+02:00 Subodh Nijsure :
> [ Sorry this is duplicate as I posted previous messages without Subject: !! ]
>
> I have following ajax query that gets generated from my template.
>
> This is done after user has logged into the system. One thing I have
> noticed is very first GET request always prompts a dialog box that
> asks users to login with username and password. I have done
> console.log and csrf_token is non-null when this dialog is shown. Does
> anybody have idea why this happens on all subsequnt reloads of this
> page I never get prompted to enter username & password.
>
> $.ajax({
> type: "GET",
> withCredentials: true,
> async: false,
> url: "/api/v1/myurl/",
> data: {
> csrfmiddlewaretoken: '{{ csrf_token }}'
> },
> success: function( json) {
> });
>
> It was suggested to me that I should follow this and make sure that
> csrf token is present in the  header.
>
>
> https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
>
> I have made sure that in my javascript I have recommended code that
> set the Requestheader but that doesn't help!
>
> Can anyone help me with this?
>
> -Subodh
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CALr9Q3a-hqk5fA1TP_3RZetv3gVh-D%2BR4kK0kBfeHBGz%3D0v9xQ%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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DJV3ZSPposHvVoCHNRNMoSXRrEPRgbRw3niSS4S_k1XJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: one form, many submits

2014-06-26 Thread Roman Klesel
>From your code:
"""
if request.method=='GET':
if request.POST.get('accept'):
bid = QuotedItem.objects.get(id=2)
return render(request, 'bills/p_bill.html', {'bid' : bid})
"""

if the method is "GET", you will never have any POST parameters.
If you want POST parameter you have to send an HTTP-POST request, but
then of course the first condition will never be true. So this code
will not work.

2014-06-26 12:53 GMT+02:00 Gunpreet Ahuja :
>
>
> On Tuesday, June 24, 2014 11:21:23 PM UTC+5:30, Gunpreet Ahuja wrote:
>>
>>
>>
>> On Saturday, February 20, 2010 9:07:57 PM UTC+5:30, David De La Harpe
>> Golden wrote:
>>>
>>> On Sat, Feb 20, 2010 at 06:42:11AM -0800, Tom wrote:
>>
>>
>>>
>>> Check request.POST.get('cancel') to see if cancel was clicked,
>>> and request.POST.get('accept') to see if accept was clicked
>
>
> I tried this, the else part works, but the interpreter never considers the
> above condition true and the control never transfers inside the if clause!
> Please help!
> Here is my views.py:
> https://gist.github.com/GunpreetAhuja/cd5093b4e3cbf594b584
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/324c5116-9030-4afb-8039-ba5f24b7d3fb%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DKed8hUAXVgznMF9bXxEwMLQFu_iktCYFo2E2ikN4S7%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: testing equality in template tag

2014-06-26 Thread Roman Klesel
Ugh , sorry of course we know ... forget what i said ...

2014-06-26 9:48 GMT+02:00 Roman Klesel <roman.kle...@gmail.com>:
> hard to tell since we do not know what values default_month and month.0 have 
> ...
> try to print in the view and examine:
> print type(month[0]), month[0], type(today.month), today.month
>
> 2014-06-25 20:34 GMT+02:00 Lee Hinde <leehi...@gmail.com>:
>> with view code like so:
>>
>> import calendar
>> months_choices = []
>> for i in range(1,13):
>> months_choices.append((i, calendar.month_name[i]))
>> context['months'] = months_choices
>>
>>
>> and
>>
>> context['default_month'] = today.month
>>
>> I have this snippet in a template (my first use of lists like this):
>>
>> 
>> {% for month in months %}
>> > value="{{ month.0 }}">
>> {{ month.1 }}
>> {% endfor %}
>> 
>>
>>
>> The issue is the  {% if default_month == month.0 %} always returns false...
>> The rest of the select is properly built - I get a number for the value and
>> the month name as the label.
>>
>> Why?
>>
>> --
>> 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 http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CA%2BePoMzx%2B4774Ne21djP-EdSAHDL_64DWhZVQYrUYdND%3DBk2%2Bw%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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DKB6MxR22LTYQjVmxiee%2BhktpkqttPmemZQhvm9r9_%3DBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: testing equality in template tag

2014-06-26 Thread Roman Klesel
hard to tell since we do not know what values default_month and month.0 have ...
try to print in the view and examine:
print type(month[0]), month[0], type(today.month), today.month

2014-06-25 20:34 GMT+02:00 Lee Hinde :
> with view code like so:
>
> import calendar
> months_choices = []
> for i in range(1,13):
> months_choices.append((i, calendar.month_name[i]))
> context['months'] = months_choices
>
>
> and
>
> context['default_month'] = today.month
>
> I have this snippet in a template (my first use of lists like this):
>
> 
> {% for month in months %}
>  value="{{ month.0 }}">
> {{ month.1 }}
> {% endfor %}
> 
>
>
> The issue is the  {% if default_month == month.0 %} always returns false...
> The rest of the select is properly built - I get a number for the value and
> the month name as the label.
>
> Why?
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2BePoMzx%2B4774Ne21djP-EdSAHDL_64DWhZVQYrUYdND%3DBk2%2Bw%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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DJo__TO_U3OKAeehv4mRzBQEH4nMf0LfNxfoEZB4%2B9j%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Handling checkbox inputs

2014-06-26 Thread Roman Klesel
Hello,

the {{j.i}} can not work as you expect. If i understand you correctly
you would like to do something like getattr(list, i) in the template.

In order to do this you have 2 possibilities:

1) Write a custom template tag like this
http://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template

2) Prepare the data in the view so that you can iterate over it more easily:

result  = []
for i in info:
  for j in list:
result.append(getattr(j, i))
return render(request, 'display.html', {'result': result})

{% for r in result %}
 {{r}}
{% endfor %}


2014-06-25 22:33 GMT+02:00 Saloni Baweja :
> I'm building a small app that at first displays the form containing the
> checkboxes and asks the user to select one or more of them and then displays
> the name of only selected ones.
> This works fine. Now, I want to display the data only corresponding to the
> fields that the user has checked or selected. This creates problem as I'm
> not able to fetch the attributes or objects from the list object named '
> info ' since it is list of unicode strings like [u'name', u'marks'] . In
> display.html {{ j.i }} is not being fetched as  ' i ' isn't an object but
> unicode string whereas ( when tried in shell ) j.name, j.marks fetches the
> information marks and name being objects or attributes.
> Templates :
>
>  # form.html
>
>
> 
>  Name 
>  Roll No. 
>  Branch 
>  Session 
>  Marks 
>  Backlog 
>   
> 
>
> # display.html
>
>
> You selected for : {{ nothing }}
> {% for i in info %}
>   {% for j in list %}
> {{ j.i }} 
>   {% endfor %}
> {% endfor %}
> {% if error %}
>   Please select atleast one field.
> {% endif %}
>
>
> # views.py
>
> def index(request):
>   return render(request, 'index.html')
>
> def display(request):
>list = Student.objects.all()
>if 'info' in request.GET:
>info = request.GET.getlist('info')
>return render(request, 'display.html', { 'info': info, 'list':list })
>else:
>
>return render(request, 'display.html', {'error': True,
> 'nothing':'nothing'})
>
>
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/523ff8cd-14e7-48d7-8c4f-28e5d0f05c3b%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3D%2BR1y1UNqy8h49whbV32OmqFTgc8%3Dgbpbk%3D6z-RH3eCHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How do I Iterate database object querysets

2014-06-24 Thread Roman Klesel
Hello,

I would recommend to create a django form and feed the post data to this
form.
Because
1) the form will extract the post data for you
2) in the form you will be able the validate and clean the post data (you
should definitively do that)

The code that saves the data to the form could also live in the form.

https://docs.djangoproject.com/en/dev/topics/forms/

If the structure of the post data matches the structure of the database
model closely, then a model form would be the easiest way.

Regards Roman


2014-06-24 21:33 GMT+02:00 G Z :

>   for lis in Licenses.objects.values('code'):
>>  for post_value in insert_data:
>>  if lis in post_value:
>>  license_tag += insert_data[lis] + ','
>>
>
> so im trying that now but the problem is django is causing issues when
> iterating over a dictionary i think because its a post dictionary
>
> Exception Value:
>
> coercing to Unicode: need string or buffer, dict found
>
> Exception Location:/home/grant/workspace/Django
> Projects/trunk/holon/portal/views.py in portal, line 171
>
>
> insert_data
>
>  [u'Unassigned'], u'vm_name_posted': [u'HyTrust-ESXi01'], u'assign_customer': 
> [u'61'], u'vm_datacenter_id_posted': [u'2'], u'vm_edit': [u'edit'], 
> u'vm_mor_posted': [u'vm-8026'], u'save': [u'save'], u'INGVMWESXI': 
> [u'INGVMWESXI'], u'insert': [u'yes'], u'customer_id': [u'1'], 
> u'csrfmiddlewaretoken': [u'4e8g52f3Hj4GmzWpQlfL8YvdJrETNZ8k'], 
> u'vm_group_id_posted': [u'1'], u'vm_id': [u'27390'], 
> u'vm_vcd_managed_posted': [u'Y'], u'vm_display_name_posted': 
> [u'HyTrust-ESXi01']}>
>
>
>
> post_value
>
> u'vm_id_posted'
>
>  --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b4eb8f2f-e98a-437d-8656-1714f958af12%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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAL2Rd%3DK_tbbfg0YB38tB_A49XE4rcGxkHUNx4azMzcb-vpKY0A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Form validation vs. DRY

2013-07-03 Thread Roman Klesel
2013/7/3 Tomas Ehrlich :

> you can use model validation
> https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects


This sounds very good! :-)

Thanks!

-- 
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Form validation vs. DRY

2013-07-03 Thread Roman Klesel
Hello,

I'm programming a database front end. In many cases I want to
constrain the data written to the database. Therefore I implement some
checks either through real database constraints or trough some logic
in the model, so it will throw an exception when the constraints are
not met.

e.g.: unique constraint on a database column.

Now, when I write my form validation, I have to implement the same
logic again, since form validation takes place before a save() is even
considered. Like this:

>>> if form.is_valid():
>>>form.save()

Is there a better way to do it? Is there a good way to avoid this
duplicate logic?

Regards
  Roman

-- 
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: deprecation of DotExpandedDict announced?

2012-12-17 Thread Roman Klesel
2012/12/17 donarb 

>
>
> https://github.com/django/django/commit/c57abd3c29cedcca00821d2a0d5708f10977f3c6
>
>


ahh the commit, thanks!

-- 
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: Custom option for Model Meta Options

2012-03-27 Thread Roman Klesel
Hello Anssi,

thanks! Seems to work. I tried it like this:

from django.db import models

class MyModelBase(models.base.ModelBase):
def __new__(cls, name, bases, attrs):
attr_meta = attrs.pop('Meta', None)

try:
has_cpk= attr_meta.has_composite_primary_key
del attr_meta.has_composite_primary_key
except AttributeError:
has_cpk = False

model = super(MyModelBase,cls).__new__(cls, name, bases, attrs)
model._meta.has_composite_primary_key = has_cpk
return model

And I then assigned this as the __metaclass__ to my Model class.

However I droped the whole composite forreign key idea in favor of
another solution.

So: Problem solved!

Thanks!
 Roman

-- 
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.



Custom option for Model Meta Options

2012-03-26 Thread Roman Klesel
Hello,

in my App I would like some Models to accept some custom Meta options like this:

class MyModel(models.Model)

  name = models.CharField(...)
  other = models.CharField(...)
  more = models.CharField(...)

  class Meta:

db_table = "my_table"
custom_option = True

It would be quite easy to accomplish this by paching the django
source. However I'd prefere to do
this by subclassing.

How to do this?

Best regards
  Roman Klesel

-- 
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: Optimization of a Large QuerySet iteration

2011-07-27 Thread Roman Klesel
2011/7/26 nixlists :

> Is it a best practice to take a bunch of code out to the stored
> procedures as much as possible, or the opposite is true with Django?

I don't want to comment on whether it's best practice or not ...

I was thinking about efficiency:
If the data processing gets too slow doing it with django, shipping
the task to the database could improve things.

On the other hand, if the speed is not the main issue. Keeping things
simple and in one place is also nice.

I just wanted to point out that usually databases are capable of doing
these things very efficiently.

Regards Roman

-- 
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: Optimization of a Large QuerySet iteration

2011-07-26 Thread Roman Klesel
Hello,

2011/7/25 nixlists :
> Is there a way to rewrite the dictionary code above to make it more
> readable, and/or rewrite everything in a more efficient manner in
> general?

I also consider myself a beginner in programming but I've been using
django for about 1 year now.

To me the code looks quite ok and straight forward.

The main question you may ask yourself may be whether or not you
really want to have django do the whole calculation thing. Many
database engines have very powerful aggregation capabilities, support
for stored procedures, functions etc.
Some of the aggregation features are available through the django orm.

But that's a design decision that may depend on various factors.

If the db-host is powerful enough however it may speed up things quite a bit.

Regards
  Roman

-- 
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: __unicode__() addition not working in basic poll application.

2011-05-17 Thread Roman Klesel
hmm... strange...

the __unicode__ thing is nothing django-specific. It works in plain
python as well:

It's supposed to work like this:

>>> class TestClass(object):
... def __unicode__(self):
... return "unicode"
... def __repr__(self):
... return "buh"
... def __str__(self):
... return "str"
...
>>> o=TestClass()
>>> o
buh
>>> print o
str
>>> print unicode(o)
unicode
>>>


2011/5/17 maaz muqri :
> im just getting "Poll object" as output

-- 
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: __unicode__() addition not working in basic poll application.

2011-05-17 Thread Roman Klesel
2011/5/16 maaz muqri :

> I am getting this
>
Poll.objects.all()
> []

What do you get when you do:

print unicode(Poll.objects.all()[0])

?

Regards
 Roman

-- 
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: referencing fields on the model without F()

2011-04-13 Thread Roman Klesel
Hello Yuka,

2011/4/13 Yuka Poppe :
> You could possibly resort to the extra() queryset method, allthough it eats
> raw sql, its documented in the manual. Im not sure if its in 0.96 though.

Yes, thanks!

I think I found a solution:

hw=Hardware.objects\
.filter(rack__he__isnull=False)\
.extra(where=['`hardware`.`he`=`hardware__rack`.`he`'])

Seems to work.

Regards Roman

-- 
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.



referencing fields on the model without F()

2011-04-13 Thread Roman Klesel
Hello,

I have an old version of django here where the F() object is not
available (0.96).

Is there a chance to reference a field on the same model without using
using F()?

I'd like to write:

hw=Hardware.objects.filter(he=F('rack__he'))

... but there is no F() in this version.

Any ideas?

Regards Roman

-- 
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: confused about ModelForm Field validation

2011-02-18 Thread Roman Klesel
2011/2/18 Roman Klesel <roman.kle...@googlemail.com>:

>> I would use pre_save signal for data modification

did not really work. In any case I need to use to_python so that the
ModelForm displays the right value, and then I'm in *BEEP*, since
to_python not only receives the values from the db but also get's
passed the return value from the FormField.clean() method...

So I still see no other way then what I'm doing in the first post.

Anyone else having a weird legacy database and a solution/idea for this?

-- 
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: confused about ModelForm Field validation

2011-02-18 Thread Roman Klesel
Hello Piotr,

2011/2/18 Piotr Zalewa :
>
> I would use pre_save signal for data modification
>

hmm ... this sounds like a good idea. It would make the whole thing a
lot more compact ... I'll give it a try.

Thank's!

Roman

-- 
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.



confused about ModelForm Field validation

2011-02-18 Thread Roman Klesel
Hello guys,

I'm running django on a legacy database and in some cases the values
stored in the tables have to be converted before the can be displayed
and also have to be converted bevore bein saved to the database.

Example:

A table for storing IP addresses:

The address field contains values that are  packed binary format
representations of the human readable value e.g:
d55f026c resolves to 213.95.2.108.

I already figured out how to convert between those tow back and forth
with the socket module.
I also figured out how I can make it work in django.

But I'm not happy with the solution I found and I suspect that there
might be a better way to do it.

Currently I'm doing it in the following way:

I have a custom ModelField:

class IpAddrField(models.CharField):
__metaclass__ = models.SubfieldBase
default_error_messages = {
'invalid' : _('%s ist keine valide IP-Adresse.')
}
def get_internal_type(self):
return "CharField"

def to_python(self, value):
print "   -- ModelField to_python got: %s" % value
value = db_to_ipaddr(value)
print "   -- ModellField to_python
returning: %s" % value

return value

def clean(self,value,model_instance):
print "   -- ModelField clean got: %s" % value
try:
value =  ipaddr_to_db(value)
except (socket.error, ValueError):
raise
exceptions.ValidationError(self.error_messages['invalid'] % value)
self.validate(value, model_instance)
self.run_validators(value)
print "   -- ModelField clean returning: %s" % value
return value

def get_prep_value(self,value):
print "   -- MoldelField get_prep got: %s" % value
value =  ipaddr_to_db(value)
print "   -- MoldelField get_prep
returning: %s" % value
return value

def formfield(self, **kwargs):
defaults = {'form_class': IpAddrFormField}
defaults.update(kwargs)
return super(IpAddrField, self).formfield(**defaults)


The code for the custom formField looks like this:

class IpAddrFormField(forms.CharField):
default_error_messages = {
'invalid' : _('%s ist keine valide IP-Adresse.')
}

def to_python(self,value):

print "   -- FormField to_python got: %s" % value

try:
value = ipaddr_to_db(value)
except socket.error:
raise
exceptions.ValidationError(self.error_messages['invalid'] % value)

print "   -- FormField to_python
returning: %s" % value

return value

def clean(self,value):
"convert the literal value to the db representation"
print "   -- FormField clean got: %s" % value

try:
value = self.to_python(value)
except socket.error:
raise
exceptions.ValidationError(self.error_messages['invalid'] % value)
self.validate(value)
self.run_validators(value)
print "   -- FormField clean return: %s" % value

return value


So when I run this code and send a form with a valid IP address it works.

Here is the log output of a successful update:

   -- ModelField to_python got:
d55f026d
   -- ModellField to_python returning: 213.95.2.109
   -- FormField clean got: 213.95.2.108
   -- FormField to_python got: 213.95.2.108
   -- FormField to_python returning:
d55f026c
   -- FormField clean return:
d55f026c
   -- ModelField to_python got:
d55f026c
   -- ModellField to_python returning: 213.95.2.108
   -- ModelField clean got: 213.95.2.108
   -- ModelField clean returning:
d55f026c
   -- ModelField to_python got:
d55f026c
   -- ModellField to_python returning: 213.95.2.108
   -- ModelField to_python got:
d55f026d
   -- ModellField to_python returning: 213.95.2.109
   -- MoldelField get_prep got: 213.95.2.108
   -- MoldelField get_prep returning:
d55f026c
 in post save (this is a post_save hook, not sure if
this is relevant here)
   -- MoldelField get_prep got: 213.95.2.108
   -- MoldelField get_prep returning:
d55f026c
 in post save  (this is a post_save hook, not sure if
this is relevant here)
 in post save  (this is a post_save hook, not 

Re: models.CharField.null=True ignored

2011-02-10 Thread Roman Klesel
2011/2/10  :
...
Thanks for the kind words. Your comments are very welcome!

-- 
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: models.CharField.null=True ignored

2011-02-10 Thread Roman Klesel
2011/2/10  :
 '' == None
> False

yes, makes sense ... I was thinking of things like:

if '':
if []:
...

which probably lead me to a wrong conclusion.

-- 
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: models.CharField.null=True ignored

2011-02-10 Thread Roman Klesel
Hello Daniel,

thanks for you reply

2011/2/10 Daniel Roseman :
>> >>> from test_django.roman.models import Jtem
>> >>> o=Jtem.objects.get(pk=29)
>> >>> o.name=''
>> >>> o.save()
>>
>> Now the record looks like this:
>>
>> mysql> select * from roman_jtem where id=29;
>> ++--+--+
>> | id | name | nachname |
>> ++--+--+
>> | 29 |      | Klesel   |
>> ++--+--+
>> 1 row in set (0.00 sec)
>>
>> This is not what I expected! The empty string should have been
>> converted into a null value!
>
> Why? What makes you think that? There is nothing that implies that should
> happen.

The setting (blank=True, null=True) make me think like that.

> You explicitly set the field to an empty string,

Yes,  which evaluates to an equivalent of None or False in python, and
with the above setting I've declared that None values should be
allowed and should be stored as NULL in the database.

> so I can't think of
> any reason why it should somehow be converted to a null.

I've mentioned the reason a bit further down in my first post:

When I start to edit the records of this table with the django
application, gradually all NULL Values will be converted into empty
string values. Because the form always posts an empty string as
None/NULL value. And if I don't take further care of converting them
back. that's just what happens. However I thought with
name=models.CharField(blank=True, null=True)
that django should convert None values to NULL.

Oh, ... no ... I just reread the docs ...
"""
 Avoid using null on string-based fields such as CharField and
TextField unless you have an
 excellent reason. If a string-based field has null=True, that means
it has two possible values
 for “no data”: NULL, and the empty string. In most cases, it’s
redundant to have two possible
 values for “no data;” Django convention is to use the empty string, not NULL.
"""

You are right... that's how it's meant to be ...

> Rather than patch Django, you could subclass CharField to do the conversion,

Yes of course. If the behavior as is, is what is wanted, that's my way
to go. I'm just wondering if this is really so... but it looks like
it...

Regards Roman

-- 
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.



models.CharField.null=True ignored

2011-02-10 Thread Roman Klesel
Hello community,

I'm using a django version 1.3 beta 1 SVN-15481 and a mysql 5 backend

I have the following field definition in my model:

class Jtem(models.Model):
name=models.CharField(blank=True, null=True, max_length=255)
nachname=models.CharField(max_length=255)

Doing manage.py syncdb  results in a table with the following definition:

CREATE TABLE `roman_jtem` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nachname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci


Now I insert some values in the table so i can run the following:

mysql> select * from roman_jtem where id=29;

++--+--+
| id | name | nachname |
++--+--+
| 29 | NULL | Klesel   |
++--+--+
1 row in set (0.00 sec)

Now I will access the table through django and save an empty string to
the name field:

>>> from test_django.roman.models import Jtem
>>> o=Jtem.objects.get(pk=29)
>>> o.name=''
>>> o.save()

Now the record looks like this:

mysql> select * from roman_jtem where id=29;
++--+--+
| id | name | nachname |
++--+--+
| 29 |  | Klesel   |
++--+--+
1 row in set (0.00 sec)

This is not what I expected! The empty string should have been
converted into a null value!

This behavior causes problems in other models where I access a legacy
database and where I update the model through a ModelForm. Because all
the null Values in the database are replaced with an empty string for
each Model I upddate.

The whole things behaves correctly (to my likings) with the following
patch to the django sources:

diff --git a/django/db/models/fields/__init__.py
b/django/db/models/fields/__init__.py
index fd0a295..569bba6 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -554,6 +554,8 @@ class CharField(Field):
 return smart_unicode(value)

 def get_prep_value(self, value):
+if self.null and not value:
+return None
 return self.to_python(value)

 def formfield(self, **kwargs):

Bug or feature?

Regards Roman

-- 
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: Error when subclassing models.ForeignKey field

2011-01-26 Thread Roman Klesel
Hello again,

since no one responded untill now, I'd like to bring this back to attention.

If no one knows an answer to my question, maybe some one could give me
some hint what I could do?

Should I open a bug report?

Thanks in advance!

Roman

-- 
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 subclassing models.ForeignKey field

2011-01-25 Thread Roman Klesel
Hello,

I'm using a current SVN checkout of django an I'm having difficulties
creating a custom field.

class ForeignKeyAX(models.ForeignKey):
__metaclass__ = models.SubfieldBase
description = "ForeignKey field prepared for Ajax Autocompletion"

def __init__(self,*args,**kwargs):
self.kunde_rel=False
if kwargs.has_key('kunde_rel'):
self.kunde_rel = kwargs.pop('kunde_rel')
super(ForeignKeyAX, self).__init__(*args,**kwargs)

with this, I get the folowing error:

KeyError at /path//id/

'ip'

Request Method: GET
Request URL:http://localhost:8000/path/id/
Django Version: 1.3 beta 1 SVN-15308
Exception Type: KeyError
Exception Value:

'ip'

Exception Location:
/path-to_djanog/django/db/models/fields/subclassing.py in __get__,
line 96


and further down:

/home/roman/devel/django-trunk/django/db/models/fields/subclassing.py in __get__

89.
  def __init__(self, field):
  self.field = field

  def __get__(self, obj, type=None):

  if obj is None:
  raise AttributeError('Can only be accessed via an instance.')

96. return obj.__dict__[self.field.name]


'ip' is not contained in obj.__dict__. Like the other foreignkey
fields its there as ip_id.

What am I doing wrong? How can I get this going?

BTW: When I ommit the line:

__metaclass__ = models.SubfieldBase

Everything works fine. But then other things I want to do like
overriding to_python will not work as far as I understand.

Please help.

Best regards
   Roman

-- 
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.