Lucas,

Thanks for your comment.

I tried your suggestion, and got this error

'module' object has no attribute 'textInput'

referring to the forms.textInput(attrs=attrs) in the widget class.

I am only trying to replace three out of 16 fields in my Inventory model
with the custom widgets. I think your solution would require me to
enumerate all the other fields as well? I found this on the Internet -
http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/,
which gave me the idea for how I wrote the form.

I could be totally off base, so this is a great learning experience for me!
Thanks for your help!

Mark


On Fri, Apr 11, 2014 at 7:37 AM, Lucas Klassmann
<[email protected]>wrote:

> Hi!
>
> I will test you code, but for now, try rewrite the form class, this way:
>
> class MeasurementForm(forms.ModelForm):
>     width = forms.DecimalField(widget=MeasurementWidget())
>     height = forms.DecimalField(widget=MeasurementWidget())
>     length = forms.DecimalField(widget=MeasurementWidget())
>
>     class Meta:
>         model = Inventory
>
> Cheers.
>
>
> On Fri, Apr 11, 2014 at 11:25 AM, Mark Phillips <
> [email protected]> wrote:
>
>> I looked at the MultiWidget and tried to implement following a couple of
>> examples, but I have run into a problem. I get this error when I try to
>> open the admin form - type object 'MeasurementWidget' has no attribute
>> 'attrs'
>>
>> widgets.py
>> class MeasurementWidget(forms.MultiWidget):
>>     def __init__(self, attrs=None):
>>         widgets = (
>>             forms.textInput(attrs=attrs),
>>             forms.textInput(attrs=attrs),
>>             forms.textInput(attrs=attrs),
>>             )
>>         super(MeasurementWidget, self).__init__(widgets, attrs)
>>
>>     def decompress(self, value):
>>         if value:
>>             frac = Fraction(value)
>>             num = frac.numerator
>>             denom = frac.denominator
>>             whole = num/denom
>>             if whole == 0:
>>                 whole = ''
>>             numerator = num - (num/denom)*denom
>>             return [whole, numerator, denom]
>>         return ['', '', '']
>>
>>     def format_output(self, rendered_widgets):
>>         return u''.join(rendered_widgets)
>>
>>     def value_from_datadict(self, data, files, name):
>>         valuelist = [widget.value_from_datadict(data, files, name + '_%s'
>> % i) for i, widget in enumerate(self.widgets)]
>>         try:
>>             if valuelist[0] == '':
>>                 valuelist[0] = 0
>>             numerator = int(valuelist[0]) * int(valuelist[2]) +
>> int(valuelist[1])
>>             denominator = int(valuelist[2])
>>             return Decimal(float(Fraction(numerator, denominator)))
>>         except ValueError:
>>             return ""
>>
>> models.py
>> class Inventory(models.Model):
>>     location = models.ForeignKey(Location)
>>     room = models.ForeignKey(Room, blank=True, null=True)
>>     condition = models.ForeignKey(Condition)
>>     status = models.ForeignKey(Status)
>>     category = models.ForeignKey(Category)
>>     length = models.DecimalField(max_digits=5, decimal_places=3,
>> default=0)
>>     width = models.DecimalField(max_digits=5, decimal_places=3, default=0)
>>     height = models.DecimalField(max_digits=5, decimal_places=3,
>> default=0)
>>     and other fields.....
>>
>> admin.py
>> class MeasurementForm(forms.ModelForm):
>>     def __init__(self, *args, **kwargs):
>>         super(MeasurementForm, self).__init__(*args, **kwargs)
>>         self.fields['width'].widget = MeasurementWidget
>>         self.fields['height'].widget = MeasurementWidget
>>         self.fields['length'].widget = MeasurementWidget
>>
>>     class Meta:
>>         model = Inventory
>>
>> class InventoryAdmin(admin.ModelAdmin):
>>     list_display = ('id','location', 'room', 'category', 'description',
>> 'condition', 'length', 'width', 'height', 'status', 'sale_price',
>> 'selling_costs', 'debt')
>>     list_filter = ['category']
>>     search_fields = ['description']
>>     form = MeasurementForm
>>     inlines = [NoteInline, ImageInline, EstimateInline]
>>
>> admin.site.register(Inventory, InventoryAdmin)
>>
>> I am really lost here. Does anyone have an easier way to override the
>> widgets on the admin form?
>>
>> Thanks,
>>
>> Mark
>>
>>
>> On Thu, Apr 10, 2014 at 7:09 PM, Lucas Klassmann <
>> [email protected]> wrote:
>>
>>> Hi!
>>>
>>> I never used this, but i found here and can be useful:
>>>
>>>
>>> https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.MultiWidget
>>>
>>> Cheers.
>>>
>>>
>>> On Thu, Apr 10, 2014 at 10:56 PM, Mark Phillips <
>>> [email protected]> wrote:
>>>
>>>> I hope I am using the right terminology....
>>>>
>>>> I have a Decimal field in a model called 'width'. In the admin screen,
>>>> I would like to display this field as three text boxes like this:
>>>>
>>>> width:  [] []/[]
>>>>
>>>> so I can enter 2 1/8 for the width, and then have the code convert that
>>>> to 2.125 as a Decimal field and store that Decimal field in the database.
>>>>
>>>> How would I go about doing this?
>>>>
>>>> Thanks,
>>>>
>>>> Mark
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at http://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-users/CAEqej2MkiPYfjO4afqyWVr587AL5UczhuzJCLhgxeqjda8%2BcDw%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAEqej2MkiPYfjO4afqyWVr587AL5UczhuzJCLhgxeqjda8%2BcDw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> Lucas Klassmann
>>> Desenvolvedor de Software
>>>
>>> Email: [email protected]
>>> Web site: http://www.lucasklassmann.com
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAOz50pLMMRv8-boG1C0Rb7vriVnpO-G953Lbh0zoRhjG6-zsLQ%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAOz50pLMMRv8-boG1C0Rb7vriVnpO-G953Lbh0zoRhjG6-zsLQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAEqej2Onin-ONGCxnC6WLpO9cL66UoENiyopp2aPns0A%3D2JkWw%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAEqej2Onin-ONGCxnC6WLpO9cL66UoENiyopp2aPns0A%3D2JkWw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Lucas Klassmann
> Desenvolvedor de Software
>
> Email: [email protected]
> Web site: http://www.lucasklassmann.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAOz50pK7Qu7YEB%3D3%2ByLRr%2Boh6nzST2o9bc0k3nt33%3DJ74-wmqA%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAOz50pK7Qu7YEB%3D3%2ByLRr%2Boh6nzST2o9bc0k3nt33%3DJ74-wmqA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2NHjOXXaC5%3Dt8PXcR_4S-Kuu5hG0d%3Dy3Q32e3SbfnS-Jg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to