Re: How to limit a ManyToManyField to three choices?

2011-03-12 Thread bagheera

Dnia 11-03-2011 o 22:45:34 greenie2600  napisał(a):


'Restaurant' instance needs to have a primary key value before a many-
to-many relationship can be used.


That is the answer You need to understand.  afiak You can't perform m2m  
validation on model level due that very reason.
That's why i did it on form level, and i think that's the only way to do  
this.  I know such things should be performed on server-side and model  
level, but i this case, is validation on form level causing any problems?


In my project i use only admin interface, so for end-user that validation  
is completely transparent.

--
Linux user

--
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: How to limit a ManyToManyField to three choices?

2011-03-11 Thread bagheera

Dnia 11-03-2011 o 21:39:01 bagheera  napisał(a):

Dnia 11-03-2011 o 21:29:29 greenie2600   
napisał(a):



bagheera -

I had seen the limit_choices_to parameter, but I thought it controlled
*which* choices are available to the user - not *how many* they're
allowed to choose.

I want to show the user a list of 20 or 30 cuisines, but forbid them
from checking more than three.

Can you show me an example of how I'd use limit_choices_to to limit
the *number* of choices the user can select?



Form validation.

this should work

class RestaurantForm(forms.ModelForm):
 cuisines = forms.ModelMultipleChoiceField(Sklep)

 class Meta:
 model = Restaurant
 def clean_sklepy(self):
 cuisines_clean = self.cleaned_data[cuisines]
 if len(cuisines_clean) > 3:
 raise forms.ValidationError('You can't choose more than  
three items!')

 return cuisines_clean





Sorry, i left some of my code :P But u got the idea.

--
Linux user

--
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: How to limit a ManyToManyField to three choices?

2011-03-11 Thread bagheera

Dnia 11-03-2011 o 21:29:29 greenie2600  napisał(a):


bagheera -

I had seen the limit_choices_to parameter, but I thought it controlled
*which* choices are available to the user - not *how many* they're
allowed to choose.

I want to show the user a list of 20 or 30 cuisines, but forbid them
from checking more than three.

Can you show me an example of how I'd use limit_choices_to to limit
the *number* of choices the user can select?



On Mar 11, 2:35 pm, bagheera  wrote:
Dnia 11-03-2011 o 18:06:43 greenie2600   
napisał(a):


> Hi all -

> I have two models with a many-to-many relationship: Restaurant and
> Cuisine. The Cuisine table contains, e.g., "Italian", "Mexican",
> "Chinese", etc. Each Restaurant record can be associated with one or
> more Cuisines.

> Here's the thing: I'd like to limit this to three Cuisines per
> Restaurant. So when editing the record for "Bob's Pan-Asian Buffet",
> the user would be able to check "Japanese", "Chinese", and "Korean",
> but wouldn't be able to check a fourth box.

> My question: can this be enforced within the model, or is this
> something I'd have to build into my interface layer?



or u can add a js script to this form field that will disallow selecting  
more than three items instead validating model (or do both).


--
Linux user

--
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: How to limit a ManyToManyField to three choices?

2011-03-11 Thread bagheera

Dnia 11-03-2011 o 21:29:29 greenie2600  napisał(a):


bagheera -

I had seen the limit_choices_to parameter, but I thought it controlled
*which* choices are available to the user - not *how many* they're
allowed to choose.

I want to show the user a list of 20 or 30 cuisines, but forbid them
from checking more than three.

Can you show me an example of how I'd use limit_choices_to to limit
the *number* of choices the user can select?



Form validation.

this should work

class RestaurantForm(forms.ModelForm):
cuisines = forms.ModelMultipleChoiceField(Sklep)

class Meta:
model = Restaurant
def clean_sklepy(self):
cuisines_clean = self.cleaned_data[cuisines]
if len(cuisines_clean) > 3:
raise forms.ValidationError('You can't choose more than  
three items!')

return cuisines_clean




--
Linux user

--
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: How to limit a ManyToManyField to three choices?

2011-03-11 Thread bagheera

Dnia 11-03-2011 o 21:23:38 greenie2600  napisał(a):


gontran -

Thanks.

However, I tried the sample code in your link, and I don't think it
will work. Returning a string from the overridden save() method
prevents the record from being saved to the database, but by the time
the save() method is invoked, my ModelForm (and consequently, I
presume, the underlying Model) has already been tested as valid. The
Restaurant isn't saved, but the form isn't redisplayed and no error
message is shown, and code execution proceeds as if the form were
valid (because it *is* valid; it just wasn't saved).

I think I need to override the model validation instead. Perhaps I
need to override Model.clean_fields()?



Right, i just get to that point, there is a cave rat about limiting  
choices on form level.

Depending on limiting query it may make problems if you edit this object.

afik U can't validate m2m fields on model level, but u can do it on form  
level and rise forms.ValidationError if needed in clean_field().


--
Linux user

--
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: How to limit a ManyToManyField to three choices?

2011-03-11 Thread bagheera

Dnia 11-03-2011 o 18:06:43 greenie2600  napisał(a):


Hi all -

I have two models with a many-to-many relationship: Restaurant and
Cuisine. The Cuisine table contains, e.g., "Italian", "Mexican",
"Chinese", etc. Each Restaurant record can be associated with one or
more Cuisines.

Here's the thing: I'd like to limit this to three Cuisines per
Restaurant. So when editing the record for "Bob's Pan-Asian Buffet",
the user would be able to check "Japanese", "Chinese", and "Korean",
but wouldn't be able to check a fourth box.

My question: can this be enforced within the model, or is this
something I'd have to build into my interface layer?




You can limit choices on model level:

http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to

If query is too complicated (cuz u want to access object's data, witch  
isn't yet available), you still can limit choices on form level, by  
overriding __init__



class RestaurantForm(forms.ModelForm):
cuisines = forms.ModelMultipleChoiceField(Sklep)

class Meta:
model = Restaurant

def __init__(self, *args, **kwargs):
super(RestaurantForm, self).__init__(*args, **kwargs)
self.fields[cuisines].queryset =  
Cuisine.objects.filter(pk__in=[fancy query])





--
Linux user

--
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: validate changes in m2m field in admin panel

2011-02-11 Thread bagheera

Dnia 11-02-2011 o 19:36:16 Piotr Zalewa  napisał(a):


I would use signals for that purpose

On 11-02-11 10:18, bagheera wrote:
Hi, i would like to perform validation on m2m field to control, if user  
made any changes. I'd like to disallow removing relations from m2m  
field, only adding them.


example:

model Product(models.Model):
name = models.TextField()
model Order(models.Model):
products = models.ManyToManyField(Product)


If user creates an order with three products, he can't remove any  
product from order after object has been saved.


Is there any way to do that on model level?

if i put in clean() method:
current_products = self.products.all() i will get list of products  
currently assigned to order. Can i get list of objects that was chosen  
in form, so i could compare them and raise validation error if needed?









Witch signal would You suggest? And where should i use it?

--
Linux user

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



validate changes in m2m field in admin panel

2011-02-11 Thread bagheera
Hi, i would like to perform validation on m2m field to control, if user  
made any changes. I'd like to disallow removing relations from m2m field,  
only adding them.


example:

model Product(models.Model):
name = models.TextField()
model Order(models.Model):
products = models.ManyToManyField(Product)


If user creates an order with three products, he can't remove any product  
from order after object has been saved.


Is there any way to do that on model level?

if i put in clean() method:
current_products = self.products.all() i will get list of products  
currently assigned to order. Can i get list of objects that was chosen in  
form, so i could compare them and raise validation error if needed?




--
Linux user

--
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: how to retreive the body text alone of a webpage

2010-10-02 Thread bagheera

Dnia 02-10-2010 o 15:10:33 jimgardener  napisał(a):


hi
I am writing an application to find out if the body text of  a web
page of given url has been modified-added or removed.Is there some way
to find out the length of body text alone?
I wrote a function that can read and return the data length.But is
there some way I can read the body text alone ?The idea is to ignore
length change due to  tracking id etc that comes with the page and
take into account only the body text.

def get_page_data(url):
f=urllib.urlopen(url)
return len(f.read())

Any help appreciated
thanks
jim



Check BS
http://www.crummy.com/software/BeautifulSoup/

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: IDE for Python/django

2010-09-27 Thread bagheera
Dnia 27-09-2010 o 15:00:51 girish shabadimath   
napisał(a):


actually i use vim for writing python scripts , i wanted IDE to easy my  
tasks of writing script (like auto-completion) ,,,i dont want GUI  
based,,i prefer  editor like vim which supports python scripts


GUI based, but lightweight "Geany" IDE is. :)
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: IDE for Python/django

2010-09-27 Thread bagheera
Dnia 27-09-2010 o 14:51:15 girish shabadimath   
napisał(a):



hi all,

is there any IDE for Python/Django ?



Most will probably recommend Eclipse + pydev plugin or Aptana + pydev.
I fell quite comfortable with NetBeans. Unfortunately it doesn't support  
django template language.


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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 in urlconf

2010-09-25 Thread bagheera
Dnia 25-09-2010 o 22:04:16 Tim Sawyer   
napisał(a):



On 25/09/10 20:39, CarloRatm wrote:

http://pastebin.com/aY6tZm6j

What's wrong with that code ?

Thank you,
cheers



^blog/ ^(?Pd+)$

should be

^blog/ ^(?P\d+)/$

??

Tim.


use raw definitions like django docs  says.
And why do u put ^ witch means beginning of the string in the middle?
That i think would be correct:

r'^articles/(?P\d+)/$'

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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 in urlconf

2010-09-25 Thread bagheera

Dnia 25-09-2010 o 21:39:15 CarloRatm  napisał(a):


http://pastebin.com/aY6tZm6j

What's wrong with that code ?

Thank you,
cheers


http://docs.djangoproject.com/en/1.2/topics/http/urls/#example

And check what ^ char mean
http://en.wikipedia.org/wiki/Regular_expression_examples


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: How to debug ORM filter stacking?

2010-09-25 Thread bagheera

Dnia 25-09-2010 o 06:17:10 ydjango  napisał(a):


I am stacking filters on queryset. For some reason the second filter
in stack is not applying and it is not giving any error either. Any
way to debug what is going on?


python manage.py shell

Import ur model, and build ur queries live. ipython has neat autoompletion  
feature. You might be able to figure out why it is working like it is.


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django 1.2.1. admin WYSIWYG editor

2010-09-22 Thread bagheera
Dnia 22-09-2010 o 14:41:56 Sithembewena Lloyd Dube   
napisał(a):


btw, it's an ordinary, pure tinyMCE, downloaded directly from their website

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django 1.2.1. admin WYSIWYG editor

2010-09-22 Thread bagheera
Dnia 22-09-2010 o 14:41:56 Sithembewena Lloyd Dube   
napisał(a):


I implemented bagheera's suggestion and I seem to have run into a  
problem. The TimnyMCE javascripts are in the admin's js folder, but when  
I view source and click on the links in Firefox, the files cannot be  
found. This is odd because the paths to the scripts are correct. For  
example actions.js is part of the default installation and I can read  
the contents via Firefox:


WRONG
tinyMCE should be placed where ordinary site media files are, not in admin  
media dir


read docs please:
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-media-definitions


It clearly states: "Keep in mind that this will be prepended with  
MEDIA_URL"


Example: i have in one of my ModelAdmin:


class Media:
js = ('js/tiny_mce/tiny_mce.js', 'js/tiny_mce/tiny_mce_config.js',)

in SETTINGS.py:
MEDIA_ROOT = '/home/bagheera/NetBeansProjects/nml-src/storage'
MEDIA_URL = '/site_media/'

so path to tiny_mce.js file would be:   
/home/bagheera/NetBeansProjects/nml-src/storage/js/tiny_mce/tiny_mce.js

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django 1.2.1. admin WYSIWYG editor

2010-09-21 Thread bagheera
Dnia 21-09-2010 o 17:41:26 Sithembewena Lloyd Dube   
napisał(a):



Hi,

Has anybody intergrated a WYSIWYG editor in the admin of a Django 1.2.1.
site? I have tried django-wysiwyg and TinyMCE to no avail. I need  
something

with concise, up-to-date documentation.

If anybody has done this, please recommend and also point to the docs?

Thanks.



I just downloaded newest tinyMCE and placed it in 'js' directory, then i  
made proper cfg


in modelAdmin:


class Media:
js = ('js/tiny_mce/tiny_mce.js', 'js/tiny_mce/tiny_mce_config.js',)

and the config in tiny_mce_config.js file:


tinyMCE.init({
mode : "textareas",
width : "800",
height : "500",
theme : "advanced",
language : 'pl',
content_css : "../../css/style.css",
theme_advanced_toolbar_location : "top",
plugins : 'preview, searchreplace, paste, table, insertdatetime, media',
theme_advanced_buttons1 :  
'preview,undo,redo,visualaid,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,cleanup,help,code',
theme_advanced_buttons2 :  
'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
theme_advanced_buttons3 :  
'forecolor,backcolor,|,sub,sup,|,charmap,insertdate,inserttime,media,|,tablecontrols'

});

and it works fine in admin panel with textareas

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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__() doesn't seem to work

2010-09-15 Thread bagheera
Dnia 15-09-2010 o 13:35:15 Hayko Karapetyan   
napisał(a):



from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

 def __unicode__(self):
return self.question


class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()

 def __unicode__(self):
return self.choice


after "pythonmanage.py syncdb" it shows "no fixtures found" as if i  
haven't



First of all, we prefer pure plain text instead html, it sometimes makes  
thing hard to read.


Code looks OK, but u must make sure that def __unicode__ is on the same  
level of indent as poll, choice, votes


so correct code would be like this:

class Ufo(models.Model):
<4spaces>name = models.CharField(max_length = 128)
<4spaces>crew_count = models.IntegerField()

<4spaces>def __unicode__(self):
<4spaces><4spaces>return self.name

U can use TABs instead, or as many spaces as u need, as long as they made  
blocks of code. Common practice is to set ur IDE to make 4 spaces by TAB  
key


read docs what fixtures are and how to use them:
http://docs.djangoproject.com/en/1.2/howto/initial-data/#providing-initial-data-with-fixtures

No fixtures are present by default


even wrote that code...and after all in 127.0.0.1/admin it doesn't show  
the

the written text it shows only the class name.spasibo)



Post traceback, if there were any errors. Do u have admin enabled in  
installed aps and urls.py?


Again, read docs
http://docs.djangoproject.com/en/1.2/ref/contrib/admin/
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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__() doesn't seem to work

2010-09-15 Thread bagheera

Dnia 15-09-2010 o 12:47:55 Hayko  napisał(a):


my django version is 1.2.1 but the  __unicode__() doesn't seem to
work what shall i do?
thanx a lot...!



Post code and traceback. We ain't wizards with crystal balls :D

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: django tinyMCE is normal text field instead of rich text formatting? a fix please.

2010-09-14 Thread bagheera
Dnia 13-09-2010 o 18:57:35 tricks...@googlemail.com   
napisał(a):



I installed Django tiny mce however i am getting a normal text area in
my admin. Can anyone help me to correct this to a rich text area where
i can access text formatting?


I just downloaded newest tinyMCE and placed it in 'js' directory, then i  
made proper cfg


in modelAdmin:


class Media:
js = ('js/tiny_mce/tiny_mce.js', 'js/tiny_mce/tiny_mce_config.js',)

and the config in tiny_mce_config.js file:


tinyMCE.init({
mode : "textareas",
width : "800",
height : "500",
theme : "advanced",
language : 'pl',
content_css : "../../css/style.css",
theme_advanced_toolbar_location : "top",
plugins : 'preview, searchreplace, paste, table, insertdatetime, media',
theme_advanced_buttons1 :  
'preview,undo,redo,visualaid,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,cleanup,help,code',
theme_advanced_buttons2 :  
'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
theme_advanced_buttons3 :  
'forecolor,backcolor,|,sub,sup,|,charmap,insertdate,inserttime,media,|,tablecontrols'

});

and it works fine in admin panel

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django webiste down

2010-09-09 Thread bagheera

Dnia 09-09-2010 o 16:43:50 Łukasz Rekucki  napisał(a):


Is it only me or is http://docs.djangoproject.com/ down?



Works fine.

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Optional foreign key with django?

2010-09-09 Thread bagheera

Dnia 09-09-2010 o 11:00:29 akaariai  napisał(a):


On Sep 8, 11:14 pm, maroxe  wrote:

Hi, In my models I want to have an optional field to a foreign key. I
tried this:

  field = models.ForeignKey(MyModel, null=True, blank=True,
default=None)

But i am getting this error:

  model.mymodel_id may not be NULL

i am using sqlite edit: if it can help, here is the exception
location:

  /usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py
in execute, line 200

so it's sqlite specific problem, i think.

PS: i droped the whole table and synced before getting this error.


This should just work (TM). Are you sure the table is dropped and
recreated correctly? The error is from the database (not from Django
code), and the reason is a not null constraint on models.mymodel_id
which should not exist. Maybe you could try this in completely clean
database?

 - Anssi



I have same problem with sqlite. Try another database. Django devs suggest  
postgres.

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: home page menu view variables cross every view?

2010-09-08 Thread bagheera

Dnia 08-09-2010 o 15:41:13 Goran  napisał(a):


Thanks for your help but I think that understand how template works.
My menu is drop down menu with menu items which comes from variable
{{ for menuitem in myvariable }}
{{ menuitem.title }}
{{ endfor }}

and myvariable is in my view:
myvariable = Item.objects.all()

So if I extend base.html menu is there, but menu items are not,
because "myvariable" come from the "view". So seems that I need
"myvariable = Item.objects.all()" in every view on the site. Which
also means that I need to repeat myself about 20 times. I was
wondering is there any solution to include myvariable in settings file
or something? Also is there any solution to extend flatpage view with
myvariable?

Thanks



http://docs.djangoproject.com/en/1.2/ref/templates/api/#subclassing-context-requestcontext

You can write ur own context processor, witch variables will be accessible  
across all templates.


Example code:
content.py file:

from nml.newsletter.forms import NewsletterForm

def newsletter_form(request):
"""
Creates unbound newsletter form
"""
nl_form = NewsletterForm()
return {'nl_form' : nl_form}

in settings.py:


TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
'static_content.newsletter_form',
)

I used that once in root template, but it is rendered with any view that  
extends my root template

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



model validation with admin.TabularInline

2010-08-20 Thread bagheera


Take a look into pool app from django tutorial. How can i make validation  
of Question model to have at least two Answer entered, if  
admin.TabularInline is used?


http://docs.djangoproject.com/en/1.2/intro/tutorial02/#adding-related-objects

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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 evaluation problem [SOLVED]

2010-08-16 Thread bagheera

Dnia 16-08-2010 o 19:25:50 Alec Shaner  napisał(a):


Regarding your issue with get_next, could be because you're invoking the
method when you define default=get_next(). Try it with just the bare  
method

name get_next.

You could also use the aggregate function instead of creating a new  
model:





Thanks! I figured that out:
from django.db.models import Max

def get_val(dictionary):
try:
return dictionary['number__max'] + 1
except:
return 0

class Newspaper(models.Model):
number = models.PositiveIntegerField(unique = True,  default = lambda  
: get_val(Newspaper.objects.aggregate(Max('number'





http://docs.djangoproject.com/en/1.2/topics/db/aggregation/#topics-db-aggregation

Just define get_next to call the Max aggregate on your Newspaper model's
number field.

On Mon, Aug 16, 2010 at 12:39 PM, bagheera  wrote:


Hi, i have "number" field in "newspaper" model.  I want assign to it
dynamically a default value, witch would be a incremented by 1 highest  
value

so far.
So i managed to make a little workaround, since i can't query for last
value of "number" field of model from model itself.

I have created new model, witch has only 1 field any 1 record, witch  
stores

current highest value.
I wrote two functions:


#-*- coding: utf-8 -*-
from nml.options.models import Wydanie_opt

def set_next(value):
   try:
   option = Wydanie_opt.objects.all()[0]
   if value > option.nr_wydania_next:
   option.nr_wydania_next = value
   option.save()
   except:
   option = Wydanie_opt(nr_wydania_next = value)
   option.save()

def get_next():
   try:
   option = Wydanie_opt.objects.all()[0]
   value = option.nr_wydania_next
   except:
   return 0
   else:
   return value + 1

Here's code of "newspaper" model:


from nml.options.utils import get_next


class Wydanie(models.Model):
   nr_wydania = models.PositiveIntegerField(verbose_name = "Numer  
wydania",

unique = True, default = get_next(),
   help_text = "Unikalny numer wydania.")

The problem is, set_next is working as intended, but get_next() does  
NOT,
 default value stays the same until i restart dev server. Why? Query  
isn't

evaluated? get_next() function isn't called at all?
Mb there is a better way to implement auto-incrementation.
Keep in mind that, "nr_wydania" field must be visible and editable,  
since
first entered value is unknown, some values may be skipped, and there  
is no

order of adding it.
--
Linux user

--
You received this message because you are subscribed to the Google  
Groups

"Django users" group.
To post to this group, send email to django-us...@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.







--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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 evaluation problem

2010-08-16 Thread bagheera

Dnia 16-08-2010 o 19:56:01 Nick  napisał(a):


How about using an autofield?

Nope. Autofield requires primary_key=True, filed need to be editable, so  
user can add values unordered, (10,11,12,13,17,25,9)


I'm checking out aggregation functions now.


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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 evaluation problem

2010-08-16 Thread bagheera
Hi, i have "number" field in "newspaper" model.  I want assign to it  
dynamically a default value, witch would be a incremented by 1 highest  
value so far.
So i managed to make a little workaround, since i can't query for last  
value of "number" field of model from model itself.


I have created new model, witch has only 1 field any 1 record, witch  
stores current highest value.

I wrote two functions:


#-*- coding: utf-8 -*-
from nml.options.models import Wydanie_opt

def set_next(value):
try:
option = Wydanie_opt.objects.all()[0]
if value > option.nr_wydania_next:
option.nr_wydania_next = value
option.save()
except:
option = Wydanie_opt(nr_wydania_next = value)
option.save()

def get_next():
try:
option = Wydanie_opt.objects.all()[0]
value = option.nr_wydania_next
except:
return 0
else:
return value + 1

Here's code of "newspaper" model:


from nml.options.utils import get_next


class Wydanie(models.Model):
nr_wydania = models.PositiveIntegerField(verbose_name = "Numer  
wydania", unique = True, default = get_next(),

help_text = "Unikalny numer wydania.")

The problem is, set_next is working as intended, but get_next() does NOT,   
default value stays the same until i restart dev server. Why? Query isn't  
evaluated? get_next() function isn't called at all?

Mb there is a better way to implement auto-incrementation.
Keep in mind that, "nr_wydania" field must be visible and editable, since  
first entered value is unknown, some values may be skipped, and there is  
no order of adding it.

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Can't build Django documentation

2010-08-14 Thread bagheera

Dnia 14-08-2010 o 05:11:25 Kevin  napisał(a):


I tried to build the Django docs and got an error.

$ cd django/docs/
$ make html
sphinx-build -b djangohtml -d _build/doctrees   . _build/html
Running Sphinx v1.0.1
loading pickled environment... not yet created
building [djangohtml]: targets for 427 source files that are out of
date
updating environment: 427 added, 0 changed, 0 removed
/Users/haitran/Desktop/obnob_project/src/django_docs/_ext/
djangodocs.py:91: DeprecationWarning: xfileref_role is deprecated, use
XRefRole
  xrefs = sphinx.roles.xfileref_role('ref', linktext, linktext,
lineno, state)
reading sources... [ 73%] ref/contrib/gis/
commands
Exception occurred:
  File "/Users/src/django_docs/_ext/djangodocs.py", line 215, in
parse_django_adminopt_node
from sphinx.directives.desc import option_desc_re
ImportError: No module named desc
The full traceback has been saved in /var/folders/od/
odl44I41FT0yZPHQw8kT8E+++TM/-Tmp-/sphinx-err-RgDvku.log, if you want
to report the issue to the developers.
Please also report this if it was a user error, so that a better error
message can be provided next time.
Either send bugs to the mailing list at ,
or report them in the tracker at . Thanks!
make: *** [html] Error 1

The process fails when it tries to do an import:

from sphinx.directives.desc import option_desc_re

Why is there no module named desc?

I am using:
Python 2.6
Sphinx 1.0.1
Django 1.2.1
Mac OS X 10.5.8



I had a same problem while ago, when i needed offline docs. So i just used  
wget to mirror http://docs.djangoproject.com/en/1.2/


http://www.devarticles.com/c/a/Web-Services/Website-Mirroring-With-wget/1/

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



generating random keys/passwords

2010-08-13 Thread bagheera
Hi, i'm looking for python module to generate random strings used as  
activation keys to being sent to newsletter subscribers in order to  
activate their subscription.

Any suggestions?


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: overwrite the save method

2010-08-11 Thread bagheera

--
class Format(models.Model):
name = models.CharField(max_length=5, unique=True)
myBoolean = models.BooleanField(default=False)

class FormFormat(forms.ModelForm):
boolean1 = forms.BooleanField(required=False)
boolean2 = forms.BooleanField(required=False)

class Meta:
model = Format
fields = ['name']

FormsetFormFormat = forms.models.modelformset_factory(Format,
max_num=0,form=FormFormat)
--
The idea is this:

if boolean1=True and boolean2=True the field myBoolean must to be True
if boolean1=True and boolean2=False the field myBoolean must to be
False
...
My question is, how i can do this?
overwriting the save method?

Well, i'm new one myself, but if i understand docs correctly, You can  
create form from model, then add b1 and b2 fields, and include only them  
for display by using 'fields' meta option.

Later, when form is valid, check b1 and b2, and set myBoolean

http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



autoincrementation of default value in admin form

2010-08-11 Thread bagheera
I have PositiveIntegerField in my model, that represents unique, new  
number of newly added element (it's newspaper no.).  It must be editable  
in admin form.
Now i would to assign a default value to the next available integer, so  
person, who will be adding new element, have that field autopopulated.


What is best way to do that?
Can i  assign callable function to 'default ' value, that will query for  
highest used integer?


Mind that it needs to be editable, because first used value is unknown,  
and some values might be skipped.


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: HttpResponseRedirect

2010-08-09 Thread bagheera

Dnia 07-08-2010 o 14:56:57 JeffH  napisał(a):


Another approach that I used recently:

# urls.py
   (r'^ThankYou/(?P\d{4})/$', views.ThankYou),

# message defs in views.py
# messages for ThankYou page
ty_messages = {
'' : 'Invalid message specified.',
'0001' : 'Pending dealer record has been re-saved, awaiting
approval.',
'0002' : 'Pending dealer record has been saved, awaiting
approval.',
...
}

# function in views.py
def ThankYou(request, ty_msg_id):
c = Context({
'request' : request,
'show_admin' : request.user.is_authenticated(),
'ty_message' : ty_messages.get(ty_msg_id,
ty_messages['']),
})
t = get_template('ThankYou.html')
html = t.render(c)
return HttpResponse(html)

# usage example

return HttpResponseRedirect('/ThankYou/0089')

--Jeff

I used messages subsystem, as suggested before, now i have nice, clen url,  
and if anybody won't be redirected from respective form, will be  
redirected from "thanks page" to root



def kontakt(request):
if request.method == 'POST':
form = KontaktForm(request.POST)
if form.is_valid():
#do sth
messages.success(request, 'Msg was send, thnks.')
return HttpResponseRedirect('/dziekujemy/')
else:
form = KontaktForm()

return render_to_response('kontakt.html', {'form': form,},
context_instance=RequestContext(request))

def dziekujemy(request):
storage = messages.get_messages(request)
if storage:
return render_to_response('dziekujemy.html',
context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/')
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: HttpResponseRedirect

2010-08-07 Thread bagheera

Dnia 07-08-2010 o 10:29:30 bagheera  napisał(a):


Dnia 06-08-2010 o 18:09:12 shacker  napisał(a):


Why not use the Messages framework for this?
http://docs.djangoproject.com/en/dev/ref/contrib/messages/

./s



That looks promising, i'll try to figure that out.


It works as intended, thanks shacker!

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: HttpResponseRedirect

2010-08-07 Thread bagheera

Dnia 06-08-2010 o 18:09:12 shacker  napisał(a):


Why not use the Messages framework for this?
http://docs.djangoproject.com/en/dev/ref/contrib/messages/

./s



That looks promising, i'll try to figure that out.
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



HttpResponseRedirect

2010-08-06 Thread bagheera
I have two pages with two different forms. Each, if validated, redirects  
to "thanks" page. I want to customize this behavior, so "thanks" page  
should display different message, regarding witch form was invoked, or  
redirects to "/' if no redirection took place (like user typed in browser  
"test.com/thanks")
Unfortunately,  HttpResponseRedirect takes only one argument. How can i  
pass right message anyway? Or mb there is some workaround, like i could  
check in "thanks" view, from what page it was redirected?



--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: django.views.static.serve issue

2010-08-05 Thread bagheera
Dnia 05-08-2010 o 14:57:04 Reinout van Rees   
napisał(a):



On 08/05/2010 01:36 PM, bagheera wrote:

Hi, i set up static files for development purposes in following way:

urls.py:


if settings.DEBUG:
urlpatterns += patterns('',(r'^site_media/(?P.*)$',
'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)

settings.py:

MEDIA_ROOT = '/home/bagheera/NetBeansProjects/nml-src/storage'
MEDIA_URL = 'http://localhost:8000/site_media/'

index.html template:



WHY i had to add "site_media" string, when it IS already included in
MEDIA_URL ?
If i use

dev server gives output:
/images/clock.gif/ HTTP/1.1" 404 2155

Full path to this file is:
'/home/bagheera/NetBeansProjects/nml-src/storage/images/clock.gif'


Probably MEDIA_URL isn't set at all in that template.  If even the  
"localhost:8000" isn't showing up...


I saw that problem once, too.  The reason was that the template is  
rendered without the proper context.  You'll need to pass along a  
RequestContext:



from django.template import RequestContext

def your_view(request):
 ...
 return render_to_response(
 your_template,
 {'some': 'parameter'},
 context_instance=RequestContext(request))


Reinout


context_instance=RequestContext(request) solves the problem, i had also  
fall back to ,  
witch now is working fine. Thanks



--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



django.views.static.serve issue

2010-08-05 Thread bagheera

Hi, i set up static files for development purposes in following way:

urls.py:


if settings.DEBUG:
urlpatterns += patterns('',(r'^site_media/(?P.*)$',  
'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)


settings.py:

MEDIA_ROOT = '/home/bagheera/NetBeansProjects/nml-src/storage'
MEDIA_URL = 'http://localhost:8000/site_media/'

index.html template:



WHY i had to add "site_media" string, when it IS already included in  
MEDIA_URL ?

If i use

dev server gives output:
/images/clock.gif/ HTTP/1.1" 404 2155

Full path to this file is:
'/home/bagheera/NetBeansProjects/nml-src/storage/images/clock.gif'
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django IDE

2010-08-05 Thread bagheera
Dnia 05-08-2010 o 00:12:41 Carlos Daniel Ruvalcaba Valenzuela  
 napisał(a):



If you are looking for a "Full IDE" I have used Eclipse+PyDev (and
other plugins) and works quite well, completion is reasonable and you
have a wealth of extensions such as VCS support, Mylyn Integration (to
work with remote task/issue managers), HTML editing, etc. It is a
little bloated if you are used to editors and other lightweight IDEs
though, but I can recommend it.

I haven't tried netbeans yet but I only hear good things of it so far.

As far commercial IDEs there is WingIDE and PyCharm, PyCharm is very
Django oriented right now and has very good autocompletion/code
editing tools, it may be the best option but Wing is also preparing
it's Django specific features for next release (available in beta
builds).



I have eclipse/aptana + PyDev but i did not found it comfortable, mb i  
just gave it too little time to get familiar with, or i'm too stick to  
NetBeans. However, i'll give a try to SPE mentioned in other post, never  
heard of that.

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Django IDE

2010-08-04 Thread bagheera
For python/django development i'm using... NetBeans! It fails to  
autocomplete items properly, but still, i find it most comfortable also  
for html, javaScript, CSS.

Sometimes i use Geany, it's really simple, yet nice IDE.
All under Linux.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



modify default admin action - delete_selected

2010-08-03 Thread bagheera


Lets say i have a Person model and a book model. Each Person can have many  
books. Person model have PositiveIntegerField that stores, how many books  
that person has. If new book is added, respective counter is incremented,  
now i want to modify default admin action - delete_selected, so it would  
change values of respective counters, but only if i confirm deletion of  
selected books.


How to override delete_seleted properly?

Sorry for my bad english.
--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: uncomplete internationalization of 'auth' in admin panel

2010-08-03 Thread bagheera
Dnia 29-07-2010 o 23:17:13 Dennis Kaarsemaker   
napisał(a):



On do, 2010-07-29 at 13:43 -0700, bagheera wrote:

Internationalization of 'auth' app in django 1.2 isn't complete. In
user edit form there are user permissions displayed in two windows,
like "auth | permission | Can add permission", etc. I want it all
fully localized, but i can't digg up right code. Any ideas how to
solve that?


These strings do not come from the code but from the database.
Unfortunately django does not yet have a built-in mechanism for
internationalizing and translating those. There are third party addons
for this, though I have no idea whether they can be used for standard
django models.



Ok, with little help, we figure most of it out:
I had to modify django/contrib/auth/management/__init__.py

def _get_all_permissions(opts):
"Returns (codename, name) for all permissions in the given opts."
perms = []
for action, action_pl in ( ('add', 'dodać'), ('change', 'zmienić'),  
('delete', 'usunąć') ):
perms.append((_get_permission_codename(action, opts), u'Może %s  
%s' % (action_pl, opts.verbose_name_raw)))

return perms + list(opts.permissions)

Now "Can add", and rest of it is translated during model creation

To use custom verbose name of 'auth' application i've created function

def get_verbose_name(app_label):
try:
return getattr(settings, "VERBOSE_APP_NAMES", {} ).get( app_label )
except:
return app_label

and changed django/contrib/auth/models.py


def __unicode__(self):
verbose_name = get_verbose_name(self.content_type.app_label)
return u"%s | %s | %s" % (
unicode(verbose_name),
unicode(self.content_type),
unicode(self.name))

Now i only have one last element, model name, witch is raw, untranslated  
verbose_name. How to change that?


1. Should i change all verbose_name strings to static, nonlocalizable  
strings?
2. Should i modify view function to pass altered strings to template,  
witch view exactly?
3. Should i change verbose_name_raw property? How to do it right? It's  
located in db/models/options.py

4. Other suggestions

--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.



uncomplete internationalization of 'auth' in admin panel

2010-07-29 Thread bagheera
Internationalization of 'auth' app in django 1.2 isn't complete. In
user edit form there are user permissions displayed in two windows,
like "auth | permission | Can add permission", etc. I want it all
fully localized, but i can't digg up right code. Any ideas how to
solve that?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.