Re: Post-save hook after inline records saved?

2011-09-08 Thread Vinicius Massuchetto
On Feb 3 2010, 8:52 pm, Andy Robinson <captainro...@gmail.com> wrote:
> I'm writing a bookkeeping system using Django latest code.   I would
> like to make sure that when certain records are saved, changes are
> made to other records.    For example, when I save a Sale model in the
> admin - or programmatic code - it may create (or update) a bunch of
> records in the general ledger tables.  Overriding the model.save()
> method worked well up to a point.
>
> I have one model that uses inlines in a master-detail relationship.
> Think of Orders, and Lines within them.  I enter an Order with 3
> Lines.  Unfortunately, overriding Order.save() doesn't work, because
> when the save event fires, none of the lines have been saved or hooked
> up to it yet.     post-save signals are no better.
>
> (Ugly workaround in admin:  hit 'save and continue editing', then hit
> 'save'.  The second time I save, the order rows exist)
>
> Can anyone suggest a strategy whereby, when an Order gets saved, I can
> hook into some event AFTER the related Lines have been saved?

Hi Andy.

Did you find a workaround for this? Or can anyone suggest something
now?

Thanks
Vinicius Massuchetto

-- 
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: Admin model listing custom ordering

2011-08-09 Thread Vinicius Massuchetto
Thanks, Christian.

After trying your attempts, I solved the problem by creating an
additional "order" field in the ForeignKey. So in the referenced model
I had to do:

class Meta:
ordering = ['fkfield__order']

That's not so elegant as I wished it to be, but it's not that ugly too, IMO.

Vinicius

2011/7/28 christian.posta <christian.po...@gmail.com>:
> Is this something that can work for you?
>
> https://docs.djangoproject.com/en/1.3/ref/models/options/#order-with-respect-to
>
> If not, then the only thing i can do is recommend you review the
> source code for how the ordering happens in the admin module for a
> model.
>
> The main spots to look at would be the get_ordering() method of the
> ChangeList class (django/contrib/admin/views/main.py) and the
> get_query_set() method of the same class. The ordering will definitely
> change, regardless of the queryset you override in the ModelAdmin
> class, but those two methods should give some clue as to what ordering
> options are available to you (default is descending by ID). you may
> wish to look closer than i did at the first line in the get_ordering()
> method which goes like this:
>        lookup_opts, params = self.lookup_opts, self.params
> there might be some additional ordering that can be accomplished with
> those variables.
>
> good luck, let me know if you figure it out
>
> On Jul 28, 6:07 am, Vinicius Massuchetto
> <viniciusmassuche...@gmail.com> wrote:
>> I got a model with a ForeignKey field, and I want the default listing
>> of this model to be ordered by this field.
>>
>> I get the correct `qs.query` SQL statement, but for some reason the
>> admin listing is not following it. This is what I've done so far:
>>
>>     class SomeAdminModel(admin.ModelAdmin):
>>         # ... fields
>>
>>         def queryset(self, request):
>>             qs = super(SomeAdminModel, self).queryset(request)
>>             fk_list = [1,2,7,3,5,6,4]
>>             ordering = 'FIELD(`fk_field_id`, %s)' % ','.join(str(id)
>> for id in fk_list)
>>             qs = qs.filter(fk_field__in = fk_list).extra(
>>                 select = {'ordering': ordering},
>>                 order_by = ('ordering',)
>>             )
>>             return qs
>>
>> What can be preventing this from behave as desired? Do I need to
>> override anything else?
>>
>> Thanks in advance.
>> --
>> Vinicius Massuchettohttp://vinicius.soylocoporti.org.br
>
> --
> 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.
>
>



-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

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



Admin model listing custom ordering

2011-07-28 Thread Vinicius Massuchetto
I got a model with a ForeignKey field, and I want the default listing
of this model to be ordered by this field.

I get the correct `qs.query` SQL statement, but for some reason the
admin listing is not following it. This is what I've done so far:

class SomeAdminModel(admin.ModelAdmin):
# ... fields

def queryset(self, request):
qs = super(SomeAdminModel, self).queryset(request)
fk_list = [1,2,7,3,5,6,4]
ordering = 'FIELD(`fk_field_id`, %s)' % ','.join(str(id)
for id in fk_list)
qs = qs.filter(fk_field__in = fk_list).extra(
select = {'ordering': ordering},
order_by = ('ordering',)
)
return qs

What can be preventing this from behave as desired? Do I need to
override anything else?

Thanks in advance.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

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



Different urlpatterns for different subdomains

2011-06-30 Thread Vinicius Massuchetto
Is there any way of conditionally choosing an urlpatterns set in
urls.py depending on the subdomain requested?

I would like to configure different Django behaviors maintaining the
same url structure according only to the subdomain.
Please let me know if this is not a good approach to achieve this.

Thanks.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

-- 
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: Autocomplete on Inlines ForeignKey

2011-05-24 Thread Vinicius Massuchetto
2011/5/24 Karen Tracey <kmtra...@gmail.com>:
> On Tue, May 24, 2011 at 6:51 AM, Vinicius Massuchetto
> <viniciusmassuche...@gmail.com> wrote:
>>
>> Have anyone been able to implement an autocomplete input field in an
>> inline formset? If so, please provide some references.
>>
>> The main apps that do this in a normal form can't render the same
>> behavior in an inline.
>>
>
> There's a ticket open (https://code.djangoproject.com/ticket/15760) for
> adding some JS hooks for dynamic inlines in admin. However your post doesn't
> mention the word dynamic or admin so I'm not sure it's what you are looking
> for...

I don't think it's exactly what I'm looking for. I need to implement
ajax completion in some fields in an inline form, it would be
something like `django-autocomplete` and `django-ajax-select` can do
in usual forms.

Any help is appreciated.
Thanks.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

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



Autocomplete on Inlines ForeignKey

2011-05-24 Thread Vinicius Massuchetto
Have anyone been able to implement an autocomplete input field in an
inline formset? If so, please provide some references.

The main apps that do this in a normal form can't render the same
behavior in an inline.

Many thanks.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

-- 
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: Readonly on subset of forms in Admin inlines

2011-05-12 Thread Vinicius Massuchetto
Hi! Did you find any solution for this? I'm trying to achieve a
similar behavior for inlines, as posted in another recent thread.

Thanks.
--
Vinicius Massuchetto

On Jan 23, 8:07 am, Peter Phillips <pxph...@gmail.com> wrote:
> Thanks for the reply!
>
> I probably didn't explain the problem properly. I have been using the
> ModelAdmin.readonly_fields option, however it hasn't been doing quite
> what I need for inline formsets. In the case of my inline form, when I
> set some of the fields toreadonly, they end up read only for all
> forms in the formset. That is, every new row that's added to the
> inline formset and all existing ones end up with read only fields.
> What I was hoping to do was override a method to dynamically set some
> fields as read only, but only for some of the forms in the inline
> formset, not all of them.
>
> The following code means that once a row/form in the inline was not
> created by the request.user, all rows/forms becomereadonly. What I'd
> like is to have fields only in the rows they did not create bereadonly, 
> leaving the ones that they did create editable.
>
> I wasn't sure whether there was perhaps an easy way to loop through
> the forms in the inline formset and set options in the admin for each
> of them separately.
>
> class IssuesInline(admin.TabularInline):
>     model = Issues
>     extra = 0
>     can_delete = False
>     fields = ('risk', 'issue', 'priority', 'l1', 'c1', 'notes')
>     exclude = ('added_by', 'modified_by')
>     readonly_fields = ('lbyc')
>
>     def get_readonly_fields(self,request,obj=None):
>         if request.user.username!=obj.added_by:
>              return ('risk', 'issue', 'priority', 'l1', 'c1',
> 'notes',) + self.readonly_fields
>         return self.readonly_fields
>
> Thanks again!
>
> On Jan 20, 8:24 pm, Thomas <tho...@googlemail.com> wrote:
>
>
>
>
>
>
>
> > Am 20.01.2011 um 03:27 schrieb Peter Phillips:
>
> > > Hello,
>
> > > Is there a straightforward way to set fields to read only for a subset
> > > of the forms in an inline formset in the Admin? I'd like to set some
> > > fields to read only on my inline form for rows that were not created
> > > by the user. However, when I set the fields to read only, it of course
> > > affects all rows/forms in the inline, preventing the user from editing
> > > their own rows. Do I need to do a custom formset for
> > > InlineModelAdmin.formset?
>
> > hi,
>
> > maybe this provides a proper solution for 
> > you:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contri...
>
> > good luck,
> > TR
>
> > 
>
> >http://thoreg.org

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



Readonly fields for existing items on admin inlines

2011-05-11 Thread Vinicius Massuchetto
Hi!

In a tabular inline, i want that the added items can't be changed or
deleted. I'm ok with the `can_delete` option, but setting
`readonly_fields` will also turn the form entry at the bottom to be
readonly too. There's another problem with some more fields that I
need to display in this form, like the user that added the entry. I
got something like this until now:

# models.py
class AbstractModel(models.Model):
user = models.ForeignKey(User, editable = False)
   ... some more fields ...
class Meta:
abstract = True

class ParentModel(AbstractModel):
... fields ...
class ChildModel(AbstractModel):
parent = models.ForeignKey(ParentModel, ... options ...)
... fields ...

# admin.py
class ChildModelInline(admin.TabularInline):
model = ChildModel
form = ChildModelForm
can_delete = False

class ParentModelAdmin(admin.ModelAdmin):
... options ...
inlines = (ChildModelInline,)

# forms.py
class ChildModelForm(models.ModelForm):
user = forms.CharField(required = False)
... some more fields and stuff needed ...

def __init__(self, *args, **kwargs):
super(ChildModelForm, self).__init__(*args, **kwargs)
try: user = User.objects.get(id = self.instance.user_id)
except: return None
self.fields['user'].initial = user.first_name
self.fields['user'].widget.attrs['readonly'] = 'readonly'

In this example I'm doing like I wanted the `user` field as readonly.

In the last line, If I change the widget attribute to ['disabled'] =
True, it works fine, but I need a text entry, not a disabled form
field. I'm also aware that I'll need to override the `save_model()`
and `save_formsets()` for this to work properly.

Many thanks.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

-- 
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: Generating fieldsets

2011-03-17 Thread Vinicius Massuchetto
2011/3/17 Vinicius Massuchetto <viniciusmassuche...@gmail.com>:
> Is it possible to generate different fieldsets for admin forms? I
> would like to retrieve different fieldsets for different users.

I managed doing this according to this procedure:
http://stackoverflow.com/questions/687829/django-overriding-get-form-to-customize-admin-forms-based-on-request

But when I use fieldsets in the admin class, there's a "Key 'foo' not
found in Form" error.

Thanks
-- 
Vinicius Massuchetto

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



Generating fieldsets

2011-03-17 Thread Vinicius Massuchetto
Is it possible to generate different fieldsets for admin forms? I
would like to retrieve different fieldsets for different users.

Thanks
-- 
Vinicius Massuchetto

-- 
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: Format DateTimeField of a model

2011-03-16 Thread Vinicius Massuchetto
2011/3/16 Daniel Roseman <dan...@roseman.org.uk>:
> On Wednesday, March 16, 2011 5:37:23 PM UTC, Vinicius Massuchetto wrote:
>>
>> I want to customize the DateTimeField returned in an admin list
>> column. I know there's the DATETIME_FORMAT setting, but I only want to
>> change in one model. I'lm also using L10N, and couldn't figure out if
>> there's a way to override what's in the default po file for my
>> language.
>>
>> What's the right way of doing that? Please note that I want to
>> preserve the sorting feature of this column.

> Create a custom admin method that returns the formatted value, and specify
> that in the `list_display` method. Make sure you set the `admin_order_field`
> property on the method to preserve the ordering functionality.
>     class MyAdmin(admin.ModelAdmin):
>         list_display = ('name', 'my_date_display')
>         def my_date_display(self, obj):
>             return obj.my_date_field.strftime('your-display-format')
>         my_date_display.admin_order_field = 'my_date_field'

Awesome. Thanks.

-- 
Vinicius Massuchetto

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



Format DateTimeField of a model

2011-03-16 Thread Vinicius Massuchetto
I want to customize the DateTimeField returned in an admin list
column. I know there's the DATETIME_FORMAT setting, but I only want to
change in one model. I'lm also using L10N, and couldn't figure out if
there's a way to override what's in the default po file for my
language.

What's the right way of doing that? Please note that I want to
preserve the sorting feature of this column.

Many thanks.
-- 
Vinicius Massuchetto

-- 
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: Convert a list to queryset and override the one in modelAdmin

2011-02-21 Thread Vinicius Massuchetto
2011/2/21 Vinicius Massuchetto <viniciusmassuche...@gmail.com>:
> 2011/2/21 Daniel Roseman <dan...@roseman.org.uk>:
>> On Monday, February 21, 2011 5:47:42 PM UTC, Vinicius Massuchetto wrote:
>
>> You can't "convert" a list to queryset, as a queryset is - as the name
>> implies - a database query.
>
> I imagined that. =/
>
>> What you could do is get all the IDs from the list and pass that into a new
>> query:
>>     my_queryset = MyModel.objects.filter(id__in=[o.id for o in my_list])
>
> I tried that, and that's the problem.
>
> The objects I want don't really "exist", as my base data is the output
> for `SomeModel.objects.annotate(n = Count(n)).filter(n = 0)` of a
> ForeignKey of the model that I want to have the queryset overrided.

As I think things are amazingly confusing here, here's a detailed
description of what I'm trying to do:

http://stackoverflow.com/questions/5056362/django-admin-change-list-to-display-null-foreignkeys-backwards

Thanks for helping.
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

-- 
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: Convert a list to queryset and override the one in modelAdmin

2011-02-21 Thread Vinicius Massuchetto
2011/2/21 Daniel Roseman <dan...@roseman.org.uk>:
> On Monday, February 21, 2011 5:47:42 PM UTC, Vinicius Massuchetto wrote:

> You can't "convert" a list to queryset, as a queryset is - as the name
> implies - a database query.

I imagined that. =/

> What you could do is get all the IDs from the list and pass that into a new
> query:
>     my_queryset = MyModel.objects.filter(id__in=[o.id for o in my_list])

I tried that, and that's the problem.

The objects I want don't really "exist", as my base data is the output
for `SomeModel.objects.annotate(n = Count(n)).filter(n = 0)` of a
ForeignKey of the model that I want to have the queryset overrided.

I'm confused how to deal with that.
Hope I made myself clear.

Thanks.
-- 
Vinicius Massuchetto

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



Convert a list to queryset and override the one in modelAdmin

2011-02-21 Thread Vinicius Massuchetto
Hi.

I know that's not exactly the proper way of building a queryset, but I
managed to build a list of objects of a model across operations and
annotations, and I want to show that an admin change list.

I'm currently declaring a queryset() method in modelAdmin, and
building this list there, but it fails to a "database error" admin
screen if I return the list.

Is there something I can use to convert it?

Thanks.
-- 
Vinicius Massuchetto

-- 
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: Local flavor and admin (zip-code)

2010-12-31 Thread Vinicius Massuchetto
On Dec 2 2008, 1:16 pm, Fabio Natali <nat...@poisson.phc.unipi.it> wrote:
> Dear Sergio, thank you very much for your reply.
>
> sergioh wrote:
>
> [...]
>
> > from django.contrib.localflavor.it.forms import ITZipCodeField
> > from django import forms
> > from django.utils.translation import ugettext_lazy as _
>
> > class DittaDipendentiAdmin(admin.ModelAdmin):
> >  cap = ITZipCodeField(_('ZIP Code'))
>
> >  class Meta:
> > model= DittaDipendenti
>
> > Just overriding the form field using the ITZipCodeField, works fine.
>
> If I write:
>
> class DittaProva(models.Model):
> cap = models.IntegerField("Cap",max_length=5)
>
> from django.contrib.localflavor.it.forms import ITZipCodeField
> from django import forms
> from django.utils.translation import ugettext_lazy as _
> class DittaProvaAdmin(admin.ModelAdmin):
> cap = ITZipCodeField(_('ZIP Code'))
> class Meta:
> model= DittaProva
>
> then I get this error at the command line:
>
> $ python manage.py syncdb
> [...]
> class DittaProvaAdmin(admin.ModelAdmin):
>   File "/home/fabio/my_django/arteak/../arteak/anagrafiche/models.py", line 
> 20, in DittaProvaAdmin
> cap = ITZipCodeField(_('ZIP Code'))
>   File 
> "/home/fabio/my_django/django-trunk/django/contrib/localflavor/it/forms.py", 
> line 18, in __init__
> max_length=None, min_length=None, *args, **kwargs)
> TypeError: __init__() got multiple values for keyword argument 'max_length'
>
> Do I miss anything? Any other tips?

I'm quite confused and stuck on this too.
As you, I want to override a model field by a django.contrib.localflavor class.

Let me know if you got any success.

Thanks!
-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

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