Sonata custom action return always integer in list field - sonata-admin

I use SonataAdminBundleversion 3.68 and i have an error within the custom action in list field.
$listMapper->add('Actions', 'HTML', ['template' => 'bundles\SonataBundle\reset_password.html.twig']);
{% extends '#SonataAdmin/CRUD/base_list_field.html.twig' %}
{% block field %}
<a class="btn btn-primary" href="{{ object.id }}">Reinit. password</a>
{% endblock %}
But in list view, field value is aways an integer with the same value of id column.
Thanks in advance for your help.
Goo day
Jérémy

Related

How can you loop through all of the children of a model?

In Django I am trying to loop through all of the children of my Todo model. But whenever I try to run it, it gives me an AttributeError that says "Manager isn't accessible via Todo instances". My code looks like this:
{% extends 'base.html' %}
{% block content %}
<h3>Tasks</h3>
{% for t in model.objects.all %} <!-- Error -->
<p>{{t.name}}</p>
{% endfor %}
{% endblock %}
{% block options %}
<li class="bg-light py-3 w-100 px-4 rounded text-nowrap fs-4">
<button class="text-decoration-none text-dark">Save</button>
</li>
{% endblock %}
I tried to just put the model in the context, and then I got an error in the HTML, so I figured out that it happened when I tried to reference 'model.objects.all'.
Inside of your view you need to specify what django should pass to the template. It does not serve the entire Database; therefore queries like in model.objects.all inside of your templates are not allowed.
Specify the queryset inside your views.py:
def todo_view(request):
context = {}
context['my_todos'] = my_todo_model.objects.all()
context['most_important_todo'] = my_todo_model.objects.get(pk=1)
# put your own logic inside the `.get` method above
return render(request, 'my_template.html' context)
And then access it inside of your template like so:
{% for t in my_todos %}
<p>{{ t.name }}</p>
{% endfor %}
{{ most_important_todo.name }}
Because we put a queryset inside of my_todos we can loop over it in the template. most_important_todo ist just a single object passed to the template, so we can access its properties (e.g. the name) directly.
Let me know how it goes

if same id is present in two tables show edit button else show assign button in django

Get all the employee profile table id and check the id with employee process,if id matches show edit button in templates else show assign button.
Views.py
def Employee(request):
emp = Emp_Profile.objects.filter(is_active=True)
emptable = Emp_Profile.objects.values_list('id')
print(emptable)
empprocess = Emp_Process.objects.values_list('username_id').distinct()
print(empprocess)
obj = {}
for i in range(len(empprocess)):
obj[i] = empprocess[i]
return render(request, 'employee.html',{'list' : emp,'empprocess':empprocess,'obj':obj})
templates
{% for list in list %}
{% if obj != list.id %}
<td>
<a href="/view_client_process/{{ list.id }}"><button
class="btn btn-info">Edit</button></a>
</td>
{% else %}
<h6>welcome</h6>
<td>
<a href="/view_client_process/{{ list.id }}"><button
class="btn btn-info">Assign</button></a>
</td>
{% endif %}
{% endfor %}
You can construct a set of username_ids and pass this to your template:
def Employee(request):
empS = Emp_Profile.objects.filter(is_active=True)
empprocess = set(Emp_Process.objects.values_list('username_id', flat=True).distinct())
return render(request, 'employee.html', {'emps' : emps, 'empprocess': empprocess })
In the template, we can then make a membership check of the set:
{% for emp in emps %}
<td>
{% if emp.id not in empprocess %}
<button class="btn btn-info">Edit</button>
{% else %}
<button class="btn btn-info">Assign</button>
{% endif %}
</td>
{% endfor %}
Note: you might want to rename your field username to user since a ForeignKey to a user is not the same as a username.
Note: please use {% url ... %} template tags [Django-doc] instead of performing URL processing yourself.

sonata admin many_to_one editable do not work

Entity definition
/**
* #var ArrayCollection|Keyword
* #ORM\ManyToMany(targetEntity="Mea\KeywordsBundle\Entity\Keyword",cascade={"persist"})
* #ORM\JoinTable(
* joinColumns={#ORM\JoinColumn(name="log_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="keyword_id", referencedColumnName="id")}
* )
*/
protected $tags;
Admin definition
protected function configureListFields(ListMapper $listMapper)
{
->add('tags','many_to_one',[
'editable' => true,
'multiple'=>true,
'class' => Keyword::class,
])
Result
Field is not editable - tags is shown as href to edit one tag.
Im search in sonata templates vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_list_field.html.twig - this field has editable true but xEditableType is null.
As you can see in the github Repository, the many-to-one list field is not editable at all.
The editable => true does nothing, and the multiple => true is not usefull because it's a many to one relation...
Here is the code of this list field :
https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Resources/views/CRUD/Association/list_many_to_one.html.twig
{% block field %}
{% if value %}
{% set route_name = field_description.options.route.name %}
{% if not field_description.options.identifier|default(false)
and field_description.hasAssociationAdmin
and field_description.associationadmin.hasRoute(route_name)
and field_description.associationadmin.hasAccess(route_name, value)
and field_description.associationadmin.id(value)
%}
<a href="{{ field_description.associationadmin.generateObjectUrl(route_name, value, field_description.options.route.parameters) }}">
{{ value|render_relation_element(field_description) }}
</a>
{% else %}
{{ value|render_relation_element(field_description) }}
{% endif %}
{% endif %}
{% endblock %}

How can I access specific array in twig for symfony?

I have a problem, I only want to access a specific location of array,
Lets say I have this code
{% set total = val.listCompanies|length%}
{% if total > 1 %}
<td>
<button id="viewcompany"
type="button"
class="pop btn btn-info"
data-toggle="popover"
title="User Company List"
data-content='
{% for key1, val1 in val.listCompanies %}
{{ val1.CompanyName }}<br>
{% endfor %}'
data-placement="right"
data-html = "true">
see company
</button>
</td>
{% endif %}
{% if total < 2 %}
<td>
{% for key1, val1 in val.listCompanies %}
<center>
{{ val1.CompanyName }}<br>
</center>
{% endfor %}
</td>
{% endif %}
I want to make a button, that if it contain only one array inside it, I dont have to use the popover button, but if it has more than 1 array inside of its, then I have to show it inside of the popover button.
The thing is I cant access the specific array, to add more logic..
If your val.listCompanies contains numeric keys, you can access the first one by using:
val.listCompanies[0]
If your val.listCompanies has generated keys and you want to access the first one, you can use:
val.listCompanies|first
If your val.listCompanies has generated keys and you want to access the Nth one, you can use:
val.listCompanies|slice(n, 1)|first
Working demo

Jinja2 in google appengine extended template repeates base templete

Ok I have a base.html and I try to use that for my header menu and footer. In my other template I loop over items and display them on the page. My problem is the the other template is repeating my base.html like it's in the loop. I hope someone can show me the error in My ways.
Here is my base.html code:
<div class="menu">
<ul class="nav">
<li>Home</li>
<li>New Entry</li>
<li>Sign-up</li>
{% if user %}
<li>{{user.name}}</li>
<li>Log-Out</li>
{% else %}
<li>Log-In</li>
{% endif %}
</ul>
​
This is in the base.html also but didn't paste correctly.
<div id="content">
{% block content %}
{% endblock %}
</div>
And here is the sub template code:
{% extends "base.html" %}
{% block content %}
{% for p in posts %}
{{ p.render() | safe }}
<br><br>
{% endfor %}
<div>
{{text}}
</div>
{% endblock %}
Please help
Edit:
edit2: removed link and found my problem I was calling the wrong html file in render()
Be kind Newbie here
Looks ok. Are you sure you don't have a loop in the python code that renders the template?

Resources