und wieder eine Frage zum Thema django.
Objektorientierung ist eine tolle Sache, mit Standardkonstruktoren und so weiter, und das noch durch django mit einer Datenbank verbunden (in meinem Fall mysql) ist noch besser.
also wieso keinen Konstruktor für die Datenbankeinträge?
Nur leider funktioniert das nicht so wie ich mir das vorgestellt habe.
Code: Alles auswählen
class Topic(models.Model): # for tabmenu entries
name = models.CharField(maxlength=60)
linkname = models.CharField(maxlength=20)
description = models.TextField()
def __init__(self,name,description,linkname):
self.name = name
self.linkname = linkname
self.description = description
self.save()
Code: Alles auswählen
In [3]: CreateDB()
---------------------------------------------------------------------------
exceptions.AttributeError Traceback (most recent call last)
/home/d3f3nd3r/Projects/comenius_py/<ipython console>
/home/d3f3nd3r/Projects/comenius_py/db/init_db.py in CreateDB()
10 User3 = User("tom","tom","tom@ad.re")
11
---> 12 t1 = Topic("Comenius","das Projekt am litec, allgmein","Comenius")
13
14 t2 = Topic("Global Warming","das GLOWA Projekt am litec","GlobalWarming")
/home/d3f3nd3r/Projects/comenius_py/db/models.py in __init__(self, name, description, linkname)
120 self.description = description
121
--> 122 self.save()
123
124 class SubTopic(models.Model): # for tabmenu entries
/var/lib/python-support/python2.4/django/db/models/base.py in save(self)
201
202 # First, try an UPDATE. If that doesn't update anything, do an INSERT.
--> 203 pk_val = self._get_pk_val()
204 pk_set = bool(pk_val)
205 record_exists = True
/var/lib/python-support/python2.4/django/db/models/base.py in _get_pk_val(self)
76
77 def _get_pk_val(self):
---> 78 return getattr(self, self._meta.pk.attname)
79
80 def __repr__(self):
AttributeError: 'Topic' object has no attribute 'id'
Hat jemand eine Idee wie ich das Implementieren kann oder ist das nicht möglich?
mfg