Re: nested forms from model with manytomany

2011-09-15 Thread Roald de Vries

On Sep 14, 2011, at 11:32 PM, Visgean wrote:


Hello  I have these two models:

class SubOrder(models.Model):
   """
   This is model for single order
   it should be used later in complex order
   """

   product = models.ForeignKey(Product)
   quantity = models.SmallIntegerField()

class Order(models.Model):
   "Model for complex order containing many SubOrder instances"

   customer = models.CharField(max_length = 20)
   day = models.DateField(auto_now = True)
   paid  = models.BooleanField(default = False)
   delivered = models.BooleanField(default = False)

   sub_orders = models.ManyToManyField(SubOrder)

   selled_by = models.ForeignKey(User)

The problem is that I want to do views for adding Order model with
possibility to add as many SubOrders as user wants. Currently I solve
this by reading  POST data... ( code:
https://github.com/A3soft/Lisculea/blob/master/Lisculea/Cafe/ 
views.py#L24

and 
https://github.com/A3soft/Lisculea/blob/master/Lisculea/Templates/cafe/order_new.djhtml
)

The problem is that I want users to be able to add SubOrders to Order
object using formset.

So I need to add/select subOrder from the same views as Order, I also
need to display Order formset and be able to edit/add suborders later.
And all SubOrders as their full widget -and not only as selection for
instance.


So, do you have any ideas how to do this without avoiding a lot of
coding - i mean using django forms and formsets?


Hi Visgean,

I think you want inline formsets: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/ 
#inline-formsets


Cheers, Roald

--
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: Setting a default value for a select widget in a modelformset

2010-09-05 Thread Roald de Vries

On Sep 2, 2010, at 4:41 PM, Steve McConville wrote:

What would be the best way to go about this? Is it necessary to create
a custom widget to achieve this?


I guess the best way would be to use the 'initial' parameter.
http://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset

Cheers, Roald

--
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: passing file from command line startup

2010-08-11 Thread Roald de Vries

On Aug 11, 2010, at 8:47 PM, Bradley Hintze wrote:

Hi all,

Is there a way that I can startup my script and pass it a file? For  
example:


~$ python myscript.py mytext.txt

and then access mytext.txt in myscript.py?


Option 1: use stdin

~$ python myscript.py < mytext.txt
>>> import sys
>>> file = sys.stdin

Option 2: read command line arguments

>>> import sys
>>> file = open(sys.argv[1])

Cheers, Roald

--
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: Adding request context to standard login/logout views?

2010-08-11 Thread Roald de Vries

On Aug 11, 2010, at 8:59 PM, Streamweaver wrote:

I use the the standard django login and logout views in the usual way

   url(r'^login/$', 'django.contrib.auth.views.login',
{'template_name': 'accounts/login.xhtml'}, "login-account"),
   url(r'^logout/$', 'django.contrib.auth.views.logout',
{'template_name': 'accounts/logout.xhtml'}, "logout-account")

My problem is that I need to add the request context to those
templates as my media URLs require the MEDIA_URL variable.  Is there a
way to add the request context


You mean http://docs.djangoproject.com/en/dev/ref/settings/ 
#std:setting-TEMPLATE_CONTEXT_PROCESSORS?


Cheers, Roald

--
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 Roald de Vries

On Aug 11, 2010, at 7:25 PM, refreegrata wrote:

My code
--
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?


Possible, but I have a feeling there might be a nicer solution. What  
do you want to happen if boolean1 is false?



I don't have idea, because boolean1 and boolean2 are form fields not
model fields. Can i pass custom parameters to the save method?


Yes, you can.

--
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: add attributes to a field without widgets

2010-08-11 Thread Roald de Vries

Hi refreegrata,

On Aug 11, 2010, at 3:31 PM, refreegrata wrote:

Hello list. I'm a newbie in django and now i am working with
"modelformset_factory".

I have something like this
--
class MyForm(forms.ModelForm):
   myField = forms.BooleanField()

   class Meta:
   model = Format
   fields = ['name']
   widgets = {'name' : forms.TextInput(attrs={ something }),}

FormsetMyForm= forms.models.modelformset_factory(Format, max_num=0,
form=MyForm)
--
With that code, django throw an error "Exception Value: () got
an unexpected keyword argument
'widget'", but i solve the problem deleting the "widget" declaration.

Now my question is: Can i add attributes in other side with Django ? a
Template for example, something like "{% field class="myclass" %}".


Your Django version is probably too old for Meta-widgets. What I do in  
this case is setting the widget attrs on form object creation:


def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# If you want an other widget:
# self.fields['name'].widget =  
forms.TextInput(attrs={something...})

self.fields['name'].widget.attrs['something'] = ...


Cheers, Roald



--
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: Search Field on All Pages

2010-08-08 Thread Roald de Vries

On Aug 8, 2010, at 2:04 PM, Tim Sawyer wrote:
What I do is to setup a search that works at /search/q=search_term,  
and then create a form on each page that submits to /search.


  



This form is in my top level site template.  The /search/ url is  
part of a search application.


There's an example of this working at http://www.brassbandresults.co.uk/

Hope that helps,

Tim.

On 08/08/10 10:41, wchildsuk wrote:

Hi,

I want a search field on all my pages and was wondering the best way
to do this. I could create a function and import it to every view but
this doesn't seem to follow the django DRY principles.


Personally, I like all of my forms to be classes. If you want that,  
you should create a context processor.


http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

Cheers, Roald

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



database object initialization versus python object initalization

2010-08-06 Thread Roald de Vries

Hi all,

I have two models (Person, PersonUpdate) with a many-to-many field  
(properties). The properties of a personupdate should be initialized  
(copied) from that of the corresponding person. But before I can add a  
list of properties to a new personupdate, it must have an id, and  
therefore be saved in the database. If I create a personupdate using  
PersonUpdate.objects.create or ~.get_or_create, I don't mind saving  
the object, but I don't want to call 'self.save()' in  
PersonUpdate.__init__. How should I handle this?


Cheers, Roald

--
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: model inheritance, abtract=True

2010-08-05 Thread Roald de Vries

On Aug 5, 2010, at 3:15 PM, Roald de Vries wrote:

On Aug 5, 2010, at 3:00 PM, Emily Rodgers wrote:



On Aug 5, 1:50 pm, Roald de Vries <downa...@gmail.com> wrote:

Dear all,

I have the following error, and don't know what it means:

Error: One or more models did not validate:
update.personupdate: 'address' has a relation with model  
Address,

which has either not been installed or is abstract.

I did a pretty big refactoring, but I think the problems started  
when
I separated my Person class into an abstract base class  
PersonProfile
and a derived class Person, and added the class PersonUpdate,  
deriving

from PersonProfile too.

Can anybody help?

Thanks in advance, cheers,

Roald


Can you provide some code snippets from your model definitions?


I had adapted the error a little, but here the real code (in Dutch)  
and the corresponding errors. I can help you with the Dutch if it's  
necessary ;-).



#
# in core.models:
#

# ...
class Persoonprofiel(models.Model):
   geslacht   = models.CharField(max_length=8, choices=(('M',  
'Man'), ('V', 'Vrouw')))

   voorletters= models.CharField(max_length=12, blank=False)
   # ...
   class Meta:
   abstract = True
# ...
class Persoon(Persoonprofiel):
   foto   = models.ImageField(upload_to="personen_fotos",  
blank=True)

   titel  = models.CharField(max_length=100, blank=True)
   # ...
   class Meta:
   verbose_name = 'Persoon'
   verbose_name_plural = 'Personen'
   ordering = ['achternaam']


###
# in update.models:
###

# ...
class PersoonUpdate(Persoonprofiel):
   persoon  = models.ForeignKey('Persoon')
   bezoekadres  = models.ForeignKey('Adres',  
related_name='persoonupdates_als_bezoekadres',  blank=True, null=True)

   # ...


#
# errors:
#

Error: One or more models did not validate:
update.persoonupdate: 'persoon' has a relation with model Persoon,  
which has either not been installed or is abstract.
update.persoonupdate: 'bezoekadres' has a relation with model Adres,  
which has either not been installed or is abstract.

# ...


Solved. Solution: omit the quotes the referenced models that foreign  
keys refer to.


--
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: model inheritance, abtract=True

2010-08-05 Thread Roald de Vries

On Aug 5, 2010, at 3:00 PM, Emily Rodgers wrote:



On Aug 5, 1:50 pm, Roald de Vries <downa...@gmail.com> wrote:

Dear all,

I have the following error, and don't know what it means:

 Error: One or more models did not validate:
 update.personupdate: 'address' has a relation with model  
Address,

which has either not been installed or is abstract.

I did a pretty big refactoring, but I think the problems started when
I separated my Person class into an abstract base class PersonProfile
and a derived class Person, and added the class PersonUpdate,  
deriving

from PersonProfile too.

Can anybody help?

Thanks in advance, cheers,

Roald


Can you provide some code snippets from your model definitions?


I had adapted the error a little, but here the real code (in Dutch)  
and the corresponding errors. I can help you with the Dutch if it's  
necessary ;-).



#
# in core.models:
#

# ...
class Persoonprofiel(models.Model):
geslacht   = models.CharField(max_length=8, choices=(('M',  
'Man'), ('V', 'Vrouw')))

voorletters= models.CharField(max_length=12, blank=False)
# ...
class Meta:
abstract = True
# ...
class Persoon(Persoonprofiel):
foto   = models.ImageField(upload_to="personen_fotos",  
blank=True)

titel  = models.CharField(max_length=100, blank=True)
# ...
class Meta:
verbose_name = 'Persoon'
verbose_name_plural = 'Personen'
ordering = ['achternaam']


###
# in update.models:
###

# ...
class PersoonUpdate(Persoonprofiel):
persoon  = models.ForeignKey('Persoon')
bezoekadres  = models.ForeignKey('Adres',  
related_name='persoonupdates_als_bezoekadres',  blank=True, null=True)

# ...


#
# errors:
#

Error: One or more models did not validate:
update.persoonupdate: 'persoon' has a relation with model Persoon,  
which has either not been installed or is abstract.
update.persoonupdate: 'bezoekadres' has a relation with model Adres,  
which has either not been installed or is abstract.

# ...




--
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 inheritance, abtract=True

2010-08-05 Thread Roald de Vries

Dear all,

I have the following error, and don't know what it means:

Error: One or more models did not validate:
update.personupdate: 'address' has a relation with model Address,  
which has either not been installed or is abstract.


I did a pretty big refactoring, but I think the problems started when  
I separated my Person class into an abstract base class PersonProfile  
and a derived class Person, and added the class PersonUpdate, deriving  
from PersonProfile too.


Can anybody help?

Thanks in advance, cheers,

Roald

--
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: Best way to present N items in table row for list

2010-07-29 Thread Roald de Vries

Hi Wadim,

On Jul 27, 2010, at 6:51 PM, Wadim wrote:

Hello.
I have a list that i want to present in a table.
Each row should have 5 items.
For now i create the function that returns me new grouped list, that i
use later in a template.
def groupListByRow(list):
cnt=0
rows=[]
while cnt

Re: Django INNER JOIN

2010-07-28 Thread Roald de Vries

On Jul 28, 2010, at 6:02 PM, kostia wrote:

Well, I used

projects = projects.filter(favourites__user = request.user)

And then I tired to order by 'date' field, which is in the Favourite
model like here:

projects = projects.filter(favourites__user =
request.user)#.order_by(filter_field)

And it throws me an error: "Cannot resolve keyword 'date' into field.
Choices are: author, benefit_description, category, creation_date,
description, favourites, id, published, resource_description, title,
video_link, votes"

favourites__date also did not help.

Any idea?


Because you're querying the Projects, you can not use Favourite fields  
as keywords. So you'll (more or less) have to stick with the other  
option.


Cheers, Roald

--
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 INNER JOIN

2010-07-28 Thread Roald de Vries

On Jul 28, 2010, at 5:19 PM, kostia wrote:

Thank you very much Roald,

What is faster:
   projects = Project.objects.filter(favourites__user = request.user)
or
   select_related('project')
?



I think it doesn't matter much, but if one is faster, it should be the  
first one. If you only use the Project object, I think you should use  
the first form for readability.


Cheers, Roald

--
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 INNER JOIN

2010-07-28 Thread Roald de Vries

Dear Kostia

On Jul 28, 2010, at 2:29 PM, kostia wrote:

I have a model Favourite with fields User, Project, Date, what means
that some user put some project as favourite on some date. I take all
favourites filtered by this user and I want to INNER JOIN that info
with Project model. How can I do that?

Something like projects = Favourite.objects.filter(user =
request.user).inner_join(Project, project)


Try not to think to relational, Django's models are OO.

To get the projects, you can do:

projects = Project.objects.filter(favourites__user = request.user)

To get something more like the join:

favourites = Favourites.objects.filter(user = request.user)

And then, if you need a project for any favourite, do  
favourite.project. If you want, you can also do:


fav_and_projs = ((f, f.project) for f in favourites)

Note that you can use "select_related('project')" if performance is an  
issue for you.



Cheers, Roald

--
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-07-18 Thread Roald de Vries

On Jul 18, 2010, at 7:19 PM, Biju Varghese wrote:

Eclipse is the best IDE for python and django.

On Jul 17, 8:53 pm, Jitendra Joshi  wrote:

What is the best open source Django IDE ?


I would say VIM, too. Emacs should be very good too, but I've never  
used it. Their advantage:
1) you can use them from the command line, so over SSH too, and they  
(at least VI) are available everywhere

2) they're so wide spread that there's a plug-in for almost everything
3) they're so configurable that you can write a plug-in to do anything  
you want

4) learn once, use for anything

Admitted, there are disadvantages: they have a learning curve,  
expecially VIM, but once you know how to use them, they increase your  
productivity a lot.


Features:
- autocompletion
- very very very strong search and replace
- macro's
- configurable key bindings/commands

Comparison of VIM and Emacs (what I've read):
- Emacs is monolithic (does everything, for example includes shell),  
VIM is unix style (does one thing well, why reproduce the shell?)

- The unix cli uses Emacs key bindings (but VI bindings are optional)
- Emacs doesn't have different (confusing) modes, VIM doesn't leave  
you with a crippled little finger (from all the 's)

- VIM has more commands than Emacs

I would say Emacs makes you more productive on shorter term, VIM makes  
you more productive on longer term.


Cheers, Roald

--
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: code repetition in views

2010-04-18 Thread Roald de Vries

On Apr 11, 2010, at 5:58 AM, ydjango wrote:

I find all my view method have identical code in start and in end:

anyway to avoid repetition...?

Example:

def typical_view_method(request):

  Check if user is authenticated.
  Get user and group
  Get some session variables

  try:
Method specific logic
  except Exception, e:
view_logger.error('error in typical_view_method:%s', e)

   response = HttpResponse(json_data, mimetype = 'application/json')
   response.__setitem__('Cache-Control', 'no-store,no-cache')
   response.__setitem__('Pragma', 'no-cache')
   response.__setitem__('Expires', '-1')
   return response


I use classes that derive from HttpResponse. Something like:


class View(HttpResponse):
def content(self):
template = loader.get_template(self.template_file)
context  = RequestContext(
self.request,
self.context_dict(),
processors=self.processors)
return template.render(context)

# ... more of such


class MyView(View):
template_file = 'my_template.html'
processors= (my_processor,)

def __init__(self, request, *args, **kwargs):
self.request, self.args, self.kwargs = request, args, kwargs
super(MyView, self).__init__(self, self.content())

def context_dict(self):
return dict(
var1 = 'var1',
var2 = 'var2',
)

# ... and so on


I will probably post a more extensive code snippet to django-dev and  
django-snippets in a few weeks.


Kind regards,

Roald

--
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 can i populate app engine(production environment) database with local sdk database

2010-04-05 Thread Roald de Vries

On Mar 31, 2010, at 9:12 AM, Eximius wrote:

Please help me out

On Mar 29, 11:04 pm, Eximius  wrote:

Hi all,
I have created a django application using app engine sdk, and have
stored data in database using forms. But the problem i am getting  
when

i deploy it, the data I stored is not there in the app engine
datastore.
So how can i populate app engine database with local database/data  
and

also vice-versa .
I am a newbie to django. I hope you will help me out
Thanks


If I understand you correctly, you're looking for dumpdata and loaddata:
http://docs.djangoproject.com/en/dev/ref/django-admin/#loaddata-fixture-fixture

--
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: Strange difference between runserver and shell

2010-03-12 Thread Roald de Vries

On Mar 11, 2010, at 11:56 AM, pyt...@roalddevries.nl wrote:

I have the following model:


   1   class MyOrderItem(models.Model):
   2   orderitem = models.ForeignKey(OrderItem, null=True,  
blank=True)

   3   # other fields
   4
   5   def save(self, *args, **kwargs):
   6   if self.orderitem and self.orderitem.order:
   7   orderitems =  
MyOrderItem.objects.filter(orderitem__order = self.orderitem.order)

   8   # ...
   9   super(MyOrderItem, self).save(*args, **kwargs)


If I execute this from `python manage.py shell`, it works fine, and  
orderitems (line 7) is a list with 3 items. But if I execute it in a  
runserver-session, self is omitted from the list, and orderitems is  
a list with 2 items.


It looks like self.orderitem hadn't been stored in the database yet.  
Moving line 9 to line 6 solves the problem.


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