0

我只是想知道 django 招聘模型的最佳设置是什么?

一个用户只能被一个用户招募,但该用户本身可能已经招募了许多其他用户。

是不是很简单:

class Recruiter(models.Model):
    recruiter = models.ForeignKey(settings.AUTH_USER_MODEL)

class Recruited(models.Model):
    recruited = models.ForeignKey(settings.AUTH_USER_MODEL)


class UserProfile(AbstractUser):

    recruited_by = models.ForeignKey(Recruiter, blank=True)
    recruited = models.ManyToManyField(Recruited, blank=True)

    objects = UserManager()

还是这样做的更好方法?

4

2 回答 2

1

我会这样做:

class Recruiter(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)

class Recruited(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    recruited_by = models.ForeignKey(Recruiter)

由于每个 Recruiteris是一个用户,每个 Recruitedis都是一个用户,但是一个 Recruitedis recruited by一个 Recruiter 和一个 Recruiter 可以have recruited0 到多个 Recruiter。在此模式中,来自 Recruiter 实例的隐式反向关系访问如下:

 recruiter_instance.recruited_set

只需查看文档:
https ://docs.djangoproject.com/en/1.4/ref/models/fields/#django.db.models.ForeignKey
https://docs.djangoproject.com/en/1.4/topics/db /queries/#backwards-related-objects
https://docs.djangoproject.com/en/1.4/ref/models/fields/#onetoonefield
如果您使用不同的 Django 版本,您可以使用版本切换 - 每个 Django 文档都有页面右下角。

于 2013-07-05T12:39:07.600 回答
1

What are you trying for? I would do that:

class UserProfile(AbstractUser):

    recruited_by = models.ForeignKey("self", blank=True, related_name="recruited")

    objects = UserManager()

Are Recruiter and Recruited classes necessaries?

于 2013-07-05T12:37:48.200 回答