Saturday, January 5, 2013

Re: How to add an element of derived data to a Django model?

Don't add it to the fields for the ModelAdmin, add it to the 'list_display':

class myModelAdmin(admin.ModelAdmin):
    list_display = ('a', 'b', 'c')


On Saturday, January 5, 2013 4:25:11 PM UTC-5, Saqib Ali wrote:

I tried Aaron's method. Django validated the model.
However, when I added property "c" to the administration interface's list of fields, It threw an error:

'myModelAdmin.fields' refers to field 'status' that is missing from the form.
Request Method:GET
Request URL:http://VVV/admin/
Django Version:1.4.3
Exception Type:ImproperlyConfigured
Exception Value:
'myModelAdmin.fields' refers to field 'status' that is missing from the form.
Exception Location:/usr/lib/python2.6/site-packages/django/contrib/admin/validation.py in check_formfield, line 396
Python Executable:/usr/bin/python
Python Version:2.6.8
Python Path:
['/WWW/XXX/YYY/ZZZ',   '/usr/lib/python26.zip',   '/usr/lib/python2.6',   '/usr/lib/python2.6/plat-linux2',   '/usr/lib/python2.6/lib-tk',   '/usr/lib/python2.6/lib-old',   '/usr/lib/python2.6/lib-dynload',   '/usr/lib/python2.6/site-packages',   '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']
Server time:Sat, 5 Jan 2013 16:17:42 -0500



On Saturday, January 5, 2013 12:16:30 PM UTC-5, Tundebabzy wrote:
I think you can write a function that returns a > b and then make it a
getter method with python's property.

i would have given an example but typing code from my phone isn't very
appealing.

Sent from my Windows Phone

-----Original Message-----
From: Saqib Ali
Sent: 1/5/2013 5:50 PM
To: django...@googlegroups.com
Subject: How to add an element of derived data to a Django model?




I have a Django Model as follows:





class myModel(models.Model):


 a = models.IntegerField()


 b = models.IntegerField()




I want to add a models.BooleanField() named c to the myModel class.
However the value of c is simply derived from a and b so I don't want
it to be stored in the Database.

I want to c to be True if a > b, otherwise False. I want c to reflect
the correct/current value even when a and b get modifed. And I want to
be able to access c using myModel.c just like I can access a and b.




Is there any way to do this?

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
 To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/d3_X6ZCWMGAJ.
 To post to this group, send email to django...@googlegroups.com.
 To unsubscribe from this group, send email to
django-users...@googlegroups.com.
 For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/bsxhNDPKEM0J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate