Hello Django-Users,
it is my first post here so i inroduce my self. I'm from berlin,
germany and working on a little django-project (one person). I choose
django because i love python and django seems the best choice. I would
classify myself as a beginner, but I have much fun with django and
become better. Sorry for my bad english.
For my project i had to import users from ldap and want to build a
hierarchy of company structure (user,departments, etc).
First Problem is the "on delete cascade" behavior. In case of
department i solved the problem by overriding the delete() method of
the model, so I can delete a department without deleting sub-
departments:
class Department(models.Model):
name = models.CharField(max_length = 70)
head = models.ForeignKey(User,blank=True,null=True)
headdepartment = models.ForeignKey
('Department',blank=True,null=True)
def delete(self):
"""Overrides the default delete-method to disable ON DELETE
CASCADE behavior"""
departments = Department.objects.filter(headdepartment = self)
print departments
for dep in departments:
dep.headdepartment = None
dep.save()
print self
super(Department,self).delete()
def __unicode__(self):
if self.headdepartment:
return '%s - %s' % (self.headdepartment,self.name)
else:
return self.name
This works fine for me, but if I delete a user, who is head of
department - the department will be delete to. One possibility is
overriding the delete() of the User-Class - and so i have to make
changes in outside of my project in django.contrib.auth.models and
this I want to avoid.
My next idea was using signals, in this case the built in pre-delete
signal - first i tried with department:
in models:
pre_delete.connect(department_callback,sender=Department)
....
def department_callback(sender, **kwargs):
department_to_delete = kwargs['instance']
departments = sender.objects.filter(headdepartment =
department_to_delete)
for dep in departments:
print dep.headdepartment
dep.headdepartment = None
dep.save()
But without success. The changes are made, but it seems that before
pre-delete is called, it is already decided which objects should be
deleted - so all sub-departments will be deleted. Why? So the pre-
delete signal makes no sense for me. Is there an other possibility to
solve the problem or are signals the wrong way?
Second problem is the user-model. Every user has an manager, which is
a user too, and belongs to a department. I extended the Usermodel
(like it is described ) in docs in this way:
class Userprofile(models.Model):
"Extending the existing integrated UserModel by a Userprofile"
user = models.ForeignKey(User, unique=True)
department = models.ForeignKey('Department', blank=True,
null=True)
phone = models.CharField(max_length=40)
But how can i add an "manager" that references a User? If i add:
manager = models.ForeignKey(User)
I got errors from django, because only on reference to a user can be
in Userprofile. Is the only possibility to change the usermodel in
django.contrib.auth.models, if i want to represent a hierarchical
structure of users?
Thank You and nice greeting from Berlin
Marco
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---