Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
How to get a value of foreign key between apps of Django?

If somebody has this kind of issue, answer is below, did it :)

I have two apps (accounts and company).

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})

P.s. Everything works well. I am able to print the user's organization with print("User organization : ", request.user.which_organization) But cannot save it with form.branch_name = request.user.which_organization in views.py. Instead of getting exact organization name of the user, created object lists all organization names...

How to achieve it?)

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Did it :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})
over 4 years ago · Santiago Trujillo Denunciar

0

Try passing an instance of the Organisation model,

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})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda