【问题标题】:given a clinic_id, how do I get the mods belonging the profile of the user给定一个clinic_id,我如何获得属于用户个人资料的mod
【发布时间】:2020-02-10 13:42:27
【问题描述】:

我真的被困在这里,我正在考虑改变我的模型并重新开始

我有这些模型

class CustomUser(AbstractBaseUser):
    email = models.EmailField(max_length=255, unique=True)
    password2 = models.CharField(max_length=128)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    active = models.BooleanField(default=True)  # Able to login
    practitioner = models.BooleanField(default=False)  # has access to a clinc
    admin = models.BooleanField(default=False)  # superuser
    staff = models.BooleanField(default=False)  # staff
    timestamp = models.DateTimeField(auto_now_add=True)


class Modalities(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name


class Profile(models.Model):
    user = models.OneToOneField(User,
                                related_name='prof_user',
                                on_delete=models.CASCADE)
    bio = models.TextField(max_length=5000)
    mods = models.ManyToManyField(Modalities)
    phone = PhoneNumberField()
    clinics = models.ManyToManyField(Clinic)
    personnummer = models.CharField(max_length=12)
    street = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    consent = models.BooleanField()


class Clinic(models.Model):
    practitioner = models.OneToOneField(User,
                                        related_name='prac_user',
                                        on_delete=models.CASCADE)
    lat = models.FloatField(null=True, blank=True)
    lng = models.FloatField(null=True, blank=True)
    name = models.CharField(max_length=128, )
    phone = PhoneNumberField()
    description = models.TextField(max_length=5000)
    street = models.CharField(max_length=128, )
    city = models.CharField(max_length=128, )

在我看来,我正在尝试从 Profile 模型中获取 mod,从 Clinic_id 开始

类似

clinic = Clinic.objects.filter(pk=clinic_id)
profile = get_object_or_404(Profile, user=request.user)
mods = profile.mods

在过去的几个小时里,我尝试了很多东西,但我就是想不通。

我需要更改我的模型还是我要访问这个错误?

【问题讨论】:

  • 您看到错误了吗?第一件事是 clinic 实际上是一个查询集,而不是一条记录。另一件事是我在Profile 模型上看不到user 字段,所以无论如何你都应该得到404
  • 我想我在兜圈子,越来越困惑。 Class Profile(models.Model): user = models.OneToOneField(User, related_name='prof_user', on_delete=models.CASCADE) 是配置文件模型中的用户字段。它指向用户模型
  • 抱歉造成混淆 - 我没有注意到 Profile 上的 user 字段。没关系,但 clinic 仍然是一个查询集,这很奇怪,因为您通过 pk 过滤所以假设返回单个记录。

标签: python django django-models orm


【解决方案1】:
profile = Profile.objects.filter(user=Clinic.objects.get(pk=clinic_id).practitioner)
mods = profile[0].mods.all() if profile else []

【讨论】:

  • 谢谢。我不得不走出半个小时。等我回家再试试这个
【解决方案2】:
clinic = Clinic.objects.get(clinic_id)
profile = get_object_or_404(Profile, user=request.user)
mods = profile.mods.filter(name__startswith=clinic.pk)

但如果 Clinic_id 为 1,则不是这样 以 10,11,111 等开头的模组也将被选中,除非您有特殊的输入方式名称。

【讨论】:

  • 我意识到我实际上并不想要会话用户的个人资料。我在个人资料行中想要的是 Clinic.practitioner 的个人资料
【解决方案3】:

我认为您的视图需要类似于:

clinic = clinic.objects.get(pk=clinic_id)
profile = profile.objects.get(clinic.practitioner)
mods = mods.objects.filter(pk__in=profile.mods.values_list('pk'))

【讨论】:

  • clinic = Clinic.objects.get(pk=clinic_id) profile = the profile of the clinic.practitioner mods = mods.objects.filter(pk__in=profile.mods.values_list('pk')) 我想我越来越近了。我现在需要的是让 Clinic.practitioner 中提到的用户
  • 您能否添加更多解释以帮助 OP 和其他人?仅代码的解释可能很神秘。
猜你喜欢
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2016-07-29
  • 2015-01-03
相关资源
最近更新 更多