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

46 views
Skip to first unread message

Babatunde Akinyanmi

unread,
Jan 5, 2013, 12:16:30 PM1/5/13
to Saqib Ali, django...@googlegroups.com
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.

Aaron C. de Bruyn

unread,
Jan 5, 2013, 12:36:09 PM1/5/13
to django...@googlegroups.com, Saqib Ali
I think you're looking for something like this on a model:

@property
def is_bigger(self):
    return self.a > self.b

-A

Saqib Ali

unread,
Jan 5, 2013, 4:25:11 PM1/5/13
to django...@googlegroups.com, Saqib Ali

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

nkryptic

unread,
Jan 5, 2013, 5:59:14 PM1/5/13
to django...@googlegroups.com, Saqib Ali
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')

Babatunde Akinyanmi

unread,
Jan 5, 2013, 9:22:50 PM1/5/13
to Saqib Ali, django...@googlegroups.com
Hi. Have a look at the django tutorial and take note of how the Poll object is_published method is added to the admin interface (I think it's the part 2 of the tutorial)


Sent from my Windows Phone

From: Saqib Ali
Sent: 1/5/2013 10:25 PM
To: django...@googlegroups.com
Cc: Saqib Ali
Subject: Re: How to add an element of derived data to a Django model?

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jW1G45cE7PIJ.

Babatunde Akinyanmi

unread,
Jan 5, 2013, 9:37:13 PM1/5/13
to nkryptic, django...@googlegroups.com, Saqib Ali
I apologize for adding noise to the thread. I didn't read this thread
before replying. Nkryptic is correct.

Sent from my Windows Phone

-----Original Message-----
From: nkryptic
Sent: 1/5/2013 11:59 PM
To: django...@googlegroups.com
Cc: Saqib Ali
Subject: 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:




https://groups.google.com/d/msg/django-users/-/bsxhNDPKEM0J.
Reply all
Reply to author
Forward
0 new messages