Login

Dynamic growing model

Author:
TonyB
Posted:
January 16, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

This model is designed for my webshop. The Client-object is very 'stretch-able' by defining more fields to the client. These extra fields ares stored in the ClientConfig-object.

Be sure to create a new Client-instance first and SAVE it! Without a client.id the ClientConfig won't work.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class CConfig(models.Model):
	name			= models.CharField(max_length=128)
	valuetype		= models.CharField(max_length=128)

	def __unicode__(self):
		return u'%s' % self.name

	class Meta:
		db_table = 'config_client'
		get_latest_by = 'id'
		ordering = ['name']

class Client(models.Model):
	login		= models.CharField(max_length=64)
	password	= models.CharField(max_length=16)
	email		= models.CharField(max_length=128)

	# firstname, surname etc. is stored in CConfig, this is designed for i8n purposes

	def __unicode__(self):
		return u'%s' % self.login

	def __getattr__( self, name ):
		# this method is called when the attribute for this class doesn't exist
		# access the shop config class!
		try:
			__cfg = CConfig.objects.get( name=name )
			__attr = self.clientconfig_set.get( config=__cfg )
		except:
			raise AttributeError
		else:
			return u'%s' % __attr.value
		raise AttributeError

	def setVar( self, name, value ):
		cfg = CConfig.objects.get( name=name )
		try:
			attr = self.clientconfig_set.get( config=cfg )
		except:
			attr = ClientConfig( config=cfg, client=self )
			attr.save()

		attr.value = value
		attr.save()

	def __setattr__( self, name, value ):
		# this method is called when the attribute for this class doesn't exist
		# access the shop config class!
		try:
			# if not found, find or create
			__cfg = CConfig.objects.get( name=name )
		except:
			# try to find attribute in normal object dict
			# setattr( self, name, value )
			self.__dict__[ name ] = value # this is old style 2.4
		else:
			self.setVar( name, value )

class ClientConfig(models.Model):
	client		= models.ForeignKey(Client)
	config		= models.ForeignKey(CConfig)
	value		= models.CharField(max_length=128)

	def __unicode__(self):
		return u'%s - %s - %s' % ( self.client, self.config, self.value )

	class Meta:
		db_table = 'client_config'
		get_latest_by = 'id'
		ordering = ['client']

if __name__ == '__main__':
	my_client = Client(email='[email protected]', login='me', password='me'
	my_client.save() # save first
	my_client.longname = 'Beaver'
	my_client.species = 'Chicken'
	my_client.save()

	# the .species and .longname must exist in the CConfig!

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.