I want to get information from another table to show in a template. It should only show one Org Name - Jeff Group
template
<tbody>
{% for org in org %}
<tr>
<td>{{ org.alias_name }}</td>
<td>
{% for orgs in orgs %}
{{ orgs.org_name }}
{% endfor %}
</td>
Let me know if you need more info.
First, I’d avoid reusing names within your for
loops. I strongly encourage you to identify the sets as plural and the instances as singular.
Beyond that, we’d need to see your view to see the context that you are supplying to the template.
login_required
def org_list(request):
org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status='oo')
orgs = Organization.objects.all()
context = {
'org': org,
'orgs': orgs,
'sponsor': request.user.pk,
}
return render(request, 'accounts/org_list.html', context)
If you only want to render one Organization
object, why are you querying all of them?
What determines which Organization
object that you want to render? That’s what you should be querying for.
org
class CustomUser(AbstractUser):
alias_email = models.EmailField(null=True, blank=True)
alias_name = models.CharField(max_length=20, unique=True, null=True, blank=True)
cell = models.CharField(blank=True, max_length=10, help_text='Cell phone number (numbers only)')
downline = models.CharField(tuple, max_length=1000000, default=1)
email_confirmed = models.BooleanField(default=False)
org_agreement_accept = models.BooleanField(default=False)
sponsor = models.BigIntegerField(default=1)
sponsor_accept = models.BooleanField(default=False, null=True, blank=True)
upline = models.CharField(tuple, max_length=1000000, default=1)
org = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True)
Ok. So you can remove the Organization
query completely, along with the reference to it in your context.
If you have an instance of CustomUser
called custom_user
, then the related Organization
is custom_user.org
.
Org Name does not show now.
<td>{{ org.alias_name }}</td>
<td>{{ org.org }}</td>
@login_required
def org_list(request):
org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status=‘oo’)
# orgs = Organization.objects.all()
context = {
'org': org,
# 'orgs': orgs,
'sponsor': request.user.pk,
}
return render(request, 'accounts/org_list.html', context)
org.org
is the reference to the Organization
object. If you want an attribute of that object, that’s what you need to render.
I have not done that before, so something like this?
def org_list(request):
org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status='oo')
orgs = Organization.objects.all()
context = {
'org': org,
'sponsor': request.user.pk,
}
return render(request, 'accounts/org_list.html', context, orgs)
Let’s say you have an instance of Organization
named organization
- perhaps retrieved as:
organization = Organization.objects.get(id=1)
.
How would you reference the name
attribute of organization
? (not necessarily in a template)
To phrase it another way, finish this python statement:
org_name =
(What would you put on the right side of the equal sign to reference the the name?)
I would think it would be organization.org_name
Yes.
So, if you have a reference to the same instance of Organization
by the name current_user.org
, how do you think you might reference the org_name
attribute?
That is where I am stuck. I can get the org value which is the key for the Organizations table key, but I can’t figure out how to get it to render the name.
Take a guess.
If you have a reference to an instance of Organization
named organization
, and you reference the org_name
attribute as organization.org_name
, what do you think the syntax might be if the name is current_user.org
instead of organization
?
tried
org.org_name
org.organization.org_name
organization.org_name
I think I am overthinking this.
Yes, I think you are.
If you have a reference to an instance of Organization
named organization
, then the name is referenced as organization.org_name
.
If your name for the reference to the same instance of Organization
is current_user.org
, then the reference to the name would be current_user.org.org_name
.
(And please, do yourself a favor and get rid of the name org
being used for an instance of CustomUser
.)
So if I understand this it should be and if so it is not displaying.
<tbody>
{% for fisher in fisher %}
<tr>
<td>{{ fisher.alias_name }}</td>
<td>{{ fisher.org.org_name }}</td>
<td>
def org_list(request):
fisher = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status=7)
context = {
'fisher': fisher,
'sponsor': request.user.pk,
}
return render(request, 'accounts/org_list.html', context)
I would verify the data and queries by printing fisher
in the view before the context definition and rendering fisher
in the template inside the loop. (And again, it’s really a bad practice to try and reuse the same variable name for both the loop identifier and the loop variable. Your context variable should logically be fishers
instead of fisher
.)
It may also help if you posted the Organization
model
Side note to the previous response - what you’re really trying to verify here is that you actually have the relationship established between CurrentUser
and Organization
, that the org
attribute is referencing the Organization
that you think it does, and that that Organization
has the org_name
field you’re expecting it to have and is not blank or null.
Those ids are correct in the db for org.
models
class Organization(models.Model):
m28t_approved = models.BooleanField(default=False)
org_name = models.CharField(max_length=50, unique=True)
org_image = models.ImageField(upload_to='org/images/', null=True, blank=True)
org_owner = models.BigIntegerField(unique=True, null=True, blank=True)
org_address = models.CharField(max_length=50, null=True, blank=True)
org_city = models.CharField(max_length=50, null=True, blank=True)
org_state = models.CharField(max_length=2, null=True, blank=True)
org_zip = models.CharField(max_length=5, null=True, blank=True)
org_phone = models.CharField(max_length=10, null=True, blank=True)
org_web = models.URLField(null=True, blank=True)
def __str__(self):
return self.org_name
class CustomUser(AbstractUser):
alias_email = models.EmailField(null=True, blank=True)
alias_name = models.CharField(max_length=20, unique=True, null=True, blank=True)
cell = models.CharField(blank=True, max_length=10, help_text='Cell phone number (numbers only)')
downline = models.CharField(tuple, max_length=1000000, default=1)
email_confirmed = models.BooleanField(default=False)
org_agreement_accept = models.BooleanField(default=False)
sponsor = models.BigIntegerField(default=1)
sponsor_accept = models.BooleanField(default=False, null=True, blank=True)
upline = models.CharField(tuple, max_length=1000000, default=1)
org = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True)