vendredi 14 novembre 2014

Django : eu un argument inattendu de mot-clé lors de l'accès ForeignKey _id-domaine dans le gestionnaire - Stack Overflow


I have a Model which looks like this:


class Mentorship (models.Model):
mentor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentor_user_id')
mentee = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentee_user_id')

objects = MentorshipManager()

def clean(self):
print(self.mentor_id) # is set and printed to stdout
print(self.mentee_id) # is set and printed to stdout

if Mentorship.objects.is_mentor(self.mentor_id, self.mentee_id):
raise ValidationError(_('This user is already a mentor.'))

The manager has a function to check if someone is already a mentor of another user, which is called while clean()ing the instance:


def is_mentor_for_goal(self, mentor_id, mentee_id, personal_goal_id):
if self.exists(mentor_id=mentor_id, mentee_id=mentee_id):
return True
else:
return False

However I always get an exception while accessing the mentor_id or mentee_id attribute in exists:


Django Version:     1.6.1
Exception Type: TypeError
Exception Value: exists() got an unexpected keyword argument 'mentor_id'

Is there a reason why I cannot access the _id field within the manager? I just don't understand why the field is accessible in the (unsaved) instance, but not in the manager.




Oh... man... It should be called


if self.filter(mentor_id=mentor_id, mentee_id=mentee_id).exists()

Thanks for your comments




A few things mentor__id will only work in queryset methods, not things like print. You should also use pk instead of id, here is how it would work:


class Mentorship(models.Model):
mentor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentor_user_id')
mentee = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentee_user_id')

objects = MentorshipManager()

def clean(self):
print(self.mentor.pk) # is set and printed to stdout
print(self.mentee.pk) # is set and printed to stdout

if Mentorship.objects.filter(mentor=self.mentor).exists():
raise ValidationError(_('This user is already a mentor.'))

def is_mentor_for_goal(self, mentor_id, mentee_id, personal_goal_id):
return self.exists(mentor__pk=mentor_id, mentee__pk=mentee_id)


I have a Model which looks like this:


class Mentorship (models.Model):
mentor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentor_user_id')
mentee = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentee_user_id')

objects = MentorshipManager()

def clean(self):
print(self.mentor_id) # is set and printed to stdout
print(self.mentee_id) # is set and printed to stdout

if Mentorship.objects.is_mentor(self.mentor_id, self.mentee_id):
raise ValidationError(_('This user is already a mentor.'))

The manager has a function to check if someone is already a mentor of another user, which is called while clean()ing the instance:


def is_mentor_for_goal(self, mentor_id, mentee_id, personal_goal_id):
if self.exists(mentor_id=mentor_id, mentee_id=mentee_id):
return True
else:
return False

However I always get an exception while accessing the mentor_id or mentee_id attribute in exists:


Django Version:     1.6.1
Exception Type: TypeError
Exception Value: exists() got an unexpected keyword argument 'mentor_id'

Is there a reason why I cannot access the _id field within the manager? I just don't understand why the field is accessible in the (unsaved) instance, but not in the manager.



Oh... man... It should be called


if self.filter(mentor_id=mentor_id, mentee_id=mentee_id).exists()

Thanks for your comments



A few things mentor__id will only work in queryset methods, not things like print. You should also use pk instead of id, here is how it would work:


class Mentorship(models.Model):
mentor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentor_user_id')
mentee = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='mentee_user_id')

objects = MentorshipManager()

def clean(self):
print(self.mentor.pk) # is set and printed to stdout
print(self.mentee.pk) # is set and printed to stdout

if Mentorship.objects.filter(mentor=self.mentor).exists():
raise ValidationError(_('This user is already a mentor.'))

def is_mentor_for_goal(self, mentor_id, mentee_id, personal_goal_id):
return self.exists(mentor__pk=mentor_id, mentee__pk=mentee_id)

0 commentaires:

Enregistrer un commentaire