【问题标题】:How to get a value of foreign key between apps of Django?如何在 Django 的应用程序之间获取外键值?
【发布时间】:2021-12-16 15:05:39
【问题描述】:

如果有人遇到这种问题,答案如下,是吗:)

我有两个应用程序(帐户和公司)。

accounts/models.py

class Organization(models.Model):
      organization_name = models.CharField(max_length=20)

#custom user model
class NewUser(AbstractBaseUser):
      which_organization = models.ForeignKey(Organization, on_delete = models.CASCADE, null=True, blank=True)
      #other fields

company/models.py

from accounts import models as accounts_model

class Branch(models.Model):
          branch_name = models.ForeignKey(
          accounts_model.Organization, on_delete = models.CASCADE, null=True, blank=True)
          #other fields

company/forms.py

from .models import Branch

class BranchForm(forms.ModelForm):
    class Meta:
        model = Branch
        fields = '__all__'

company/views.py

from .forms import BranchForm

def some_function(request):
    form = BranchForm()
    if request.method == 'POST':
       form = BranchForm(request.POST, request.FILES)
       if form.is_valid():
          form.save(commit=False)
          form.branch_name = request.user.which_organization  
          print("User organization: ", request.user.which_organization)
          form.save()
    return render(request, 'company/index.html', {'form': form})

附:一切正常。我可以打印用户的组织 print("User organization : ", request.user.which_organization) 但不能用 form.branch_name = request.user.which_organization 在views.py中创建的对象不是获取用户的确切组织名称,而是列出所有组织名称...

如何实现?)

【问题讨论】:

    标签: python-3.x django django-models django-views foreign-keys


    【解决方案1】:

    尝试传递一个组织模型的实例,

    def some_function(request):
        form = BranchForm()
        if request.method == 'POST':
           form = BranchForm(request.POST, request.FILES)
           if form.is_valid():
              form.save(commit=False)
              organisation_instance = Organisation.objects.get(id = request.user.which_organization.id )  
              form.branch_name = organisation_instance  
              print("User organization: ", request.user.which_organization)
              form.save()
        return render(request, 'company/index.html', {'form': form})
    

    【讨论】:

    • 您好,感谢您的回复!同样的结果再次;(
    • 你能再解释一下你想要什么以及导致的问题是什么
    • 我有一个名为accounts的应用程序,另一个是公司。 1. 在帐户应用程序中,我创建了自定义用户模型,该模型具有字段 which_organization ForeignKey 字段(与同一应用程序中的组织模型类连接,仅包含组织名称 CharField)。 2. 在公司应用程序的模型中,我只有一个名为 Branch 的模型类,其中包含 branch_name(我的意思是组织名称,取决于这家公司)ForeignKey 字段(连接到帐户应用程序中的自定义用户类)。
    • 在控制台保存表单之前尝试打印出organization_instance和organization_instance.organization_name,并检查是否正在访问正确的实例
    • 我想把用户所属的组织名称保存到Branch模型中,就这样)
    【解决方案2】:

    做到了吗:D

    def some_function(request):
        form = BranchForm()
        if request.method == 'POST':
           form = BranchForm(request.POST, request.FILES)
           if form.is_valid():
              another_form = form.save(commit=False)
              another_form.branch_name =  Organisation.objects.get(id= request.user.which_organization.id )  
              new_form.save()
        return render(request, 'company/index.html', {'form': form})
    

    【讨论】:

    • 而不是使用查询检索当前用户的组织数据。你可以使用request.user.which_organization
    • 我想再补充一件事。每当从同一项目中的另一个应用程序导入模型时,最好这样做from app_name.models import ModelName
    • 是的,在我将其更改为从 app.model 导入模型之后。
    • 使用request.user.which_organization 检索组织名称在打印时完成了这项工作,但无论何时发生保存事件,它都不会保存在数据库中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2021-10-25
    • 2016-07-11
    • 2021-02-13
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多