I have models:
class Model_1(models.Model):
name = models.CharField(...
pos = models.IntegerField(...
class Model_2(models.Model):
...
m1 = models.ManyToManyField(Model_1,...
and I print this in template:
{% for m2 in model2 %}
{% for m1 in m2.m1.all %}
{{ m1.name }}
{% endfor %}
{% endfor %}
but I want to print m1.names ordered by 'pos' field. How to do it?
in the model you can specify ordering of the objects. More here
class M1():
...
class Meta:
ordering = ['pos']
Related
I have django models like this:
class FittingBag(models.Model):
...
fittings = models.ManyToManyField("app.Fitting", through="FittingInBagInstances")
class FittingInBagInstances(models.Model):
fitting = models.ForeignKey("app.Fitting", on_delete=models.CASCADE)
bag = models.ForeignKey("app.FittingBags", on_delete=models.CASCADE)
qty = models.DecimalField(verbose_name='Quantity' );
Is there any way to access intermediate fields (like "qty" ) from Django template without preparing the date in the view?
You can simply iterate over the releverse relation that is constructed by the many-to-many intermediate model:
{% for fittingbag in fittingbags %}
Rendering the fittingbag: {{ fittingbag }}
{% for fittinginbag in fittingbag.fittinginbaginstances_set.all %}
qty: {{ fittinginbag.qty }}
fitting: {{ fittinginbag.fitting }}
{% endfor %}
{% endfor %}
I am trying to iterate over my FKs in my model such that I show all the connections through various tables. My template renders but does not show any values. Any ideas?
models.py
class State(models.Model):
state = models.CharField(max_length=255)
relevantdisease = models.ForeignKey(Disease)
relevantoption = models.ManyToManyField(Option, through='StateOption')
class StateOption(models.Model):
partstate = models.ForeignKey(State)
partoption = models.ForeignKey(Option)
relevantoutcome = models.ManyToManyField(Outcome, through='StateOptionOutcome')
class StateOptionOutcome(models.Model):
stateoption = models.ForeignKey(StateOption)
relevantoutcome = models.ForeignKey(Outcome)
outcomevalue = models.CharField(max_length=20)
views.py
def stateall(request, disease_id):
disease = get_object_or_404(Disease, pk=disease_id)
states = State.objects.select_related().filter(relevantdisease=disease_id)
context = {'disease':disease,'states': states}
return render(request, "stateall.html", context)
template.html
{% for state in states %}
<li>{% for i in state.stateoption_set.all %}</li>
<li>{% for j in i.stateoptionoutcome_set.all %}</li>
{% endfor %}
{% endfor %}
{% endfor %}
I would like the template to show up as:
State1<state>
<li>partoption</li>
<li>relevantoutcome: outcomevalue</li>
State2<state>
<li>partoption</li>
<li>relevantoutcome: outcomevalue</li>
...
Your template never outputs anything.
You're probably misunderstanding the use of the {% for %} template tag.
This:
<li>{% for j in i.stateoptionoutcome_set.all %}</li>
{% endfor %}
Outputs <li> a few times.
But this:
{% for j in i.stateoptionoutcome_set.all %}
<li>{{ j.relevantoutcome }}: {{ j.outcomevalue }}</li>
{% endfor %}
Will output a line per StateOptionOutcome found in i.stateoptionoutcome_set.all.
I have this models:
class House(models.Model):
name = models.CharField()
class BedRoom(models.Model):
name = models.CharField()
class Catalog(models.Model):
name = models.CharField()
house = models.ForeignKey(House)
bedroom = models.ForeignKey(BedRoom)
at admin.py Catalog is inline to House
class CatalogInline(admin.TabularInline):
model = Catalog
class HomeAdmin(admin.ModelAdmin):
inline = [CatalogInline]
then I need obtain at view the list of Bedrooms from House Catalog, at Home model I have:
def bedrooms(self):
return self.catalog_set.all()
but when I do at template obtaining "houses" from a view:
{% for house in houses %}
{% for h in house %}
<p>{{h.name}}</p>
{% endfor %}
{% endfor %}
I get an error:
'Catalog' object is not iterable
what I'm doing wrong?
I should define the model in a different way?
As I mentioned, you don't seem to actually be calling the bedrooms method. I guess (and it is just a guess) you mean this:
{% for house in houses %}
{% for h in house.bedrooms %}
<p>{{h.name}}</p>
{% endfor %}
{% endfor %}
The bedrooms method is pointless though, as you can just as easily do this:
{% for house in houses %}
{% for h in house.catalog_set.all %}
<p>{{h.name}}</p>
{% endfor %}
{% endfor %}
I have 4 models
Class A
name
Class B
fk1 = FK(Class A)
Class C
fk = FK(Class B)
Class D
fk = FK(Class C)
And in templates i want to use reverse relationship over all these models
I tried this
{% for que in Class A items %}
{% for item in que.b.c.d_set.all %}
but note getting the result. Any suggestions?
If I understood the question will be something like:
{% for que in a_times %}
{% for b_item in que.b_set.all %}
{% for c_item in b_item.c_set.all %}
{% for d_item in c_item.d_set.all %}
# stuff with d_item
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
That is because you are using ForeignKey instead of OneToOneField, and I suggest you use related_name
In the view you can do this:
from app.models import A, D
from django.shortcuts import render
def view(request):
data = {}
a_itemsqs = A.objects.all() # here you filter A objects
d_items = D.objects.filter(c__b__a__in=a_itemsqs)
data['d_items'] = d_items
return render(request, data, 'template.html')
I have two classes:
class People
name = CharField()
class Equipment
name = Charfield()
responsible = ForeignKey(People)
and view:
def persone_detail(request, tab_number):
return direct_to_template(request, 'person.html', {
'persone': Peoples.objects.filter(tab_number=tab_number)
How can I show in template the name from equipment?
By following the reverse relationship to Equipment:
http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
{% for person in persone %}
Person: {{ person.name }}
{% for equipment in person.equipment_set.all %}
Equipment: {{ equipment.name }}
{% endfor %}
{% endfor %}