Read the fields of the container of a collection - collections

I have a container for example: students.{title, description, children=student} and student={title, ...}
# students.ini
[model]
name = Students
[fields.title]
label = Title
type = string
[fields.description]
label = Description
type = markdown
[children]
model = student
# student.ini
[model]
name = Student
[fields.title]
label = Title
type = string
I would like to get the title/description of the studentS container.
{% set root = site.get('/students') %}
{{ root.description }}
But with get or query, I get the list and not the container...
Any idea?
Thank you

Sure, you have to access the record attribute first:
{{ site.get('/students').record.description }}
... and you can always access the .parent attribute of a child.

Related

Combobox doesn't display the populated values

I created a combobox to show a list of values out of a sqlite database.
If I sent the values to the combobox, the list will shown. The problem is, that the field will not filled with the first value and stay empty until I select on item of out of the drop menu.
Could I set a value to be displayed directly?
Here my code snippet:
self.e_business = ttk.Combobox(address_frame, width = 40)
self.e_business.grid(row=3, column=1, columnspan=2, padx=(5,20), pady=(15,5), sticky='WE')
The function which send the values:
def show_name_search(self, event):
...
widget = event.widget
selection = widget.curselection()
indName = widget.get(selection[0])
print(indName)
print("selktierter Wert: {}".format(indName))
self.realName.set(indName)
connection = sqlite3.connect(select_connect_db)
print('Database connected.')
with connection:
cursor = connection.cursor()
cursor.execute("SELECT number, type, prio, id, uniqueid FROM numbers WHERE realName='"+indName+"';")
data = cursor.fetchall()
print(data)
cache = []
for row in data:
cache.append(row[0])
self.e_business['values'] = cache
The cache list looks like:
CACHE: ['081191310912', '071109131090', '01754123353']
Define the list in the __init__() and then populate inside the function, like:
def __init__(self):
self.cache = []
... # Rest of codes
def show_name_search(self,event):
....# Rest of codes
for row in data:
self.cache.append(row[0])
self.e_business['values'] = self.cache # Set the value to the new list
self.e_business.current(0) # Set the first item of the list as current item
To set the item to the current item, you can use current() and the index of the item to be set first. Note that the indentation levels will change according to your class.

How can I add a CSS Class to a Flask-Table Column

I want to add CSS Classes to specific Columns, the Table is made via Flask-Table. But I only found how to add Classes to the whole table is there a way to add classes to a Column too?
class Table(Table):
classes = ["table", "table-hover", "clickable-row", "sortable"]
username = Col("Username")
vorname = Col("Vorname")
nachname = Col("Nachname")
gebdat = DatetimeCol("Gebdat", datetime_format="dd.MM.yyyy")
admin = BoolCol("Rolle", yes_display='Admin', no_display='Benutzer')
aktiv = BoolCol("Status", yes_display='aktiviert',
no_display='deaktiviert')
edit = LinkCol("Bearbeiten", 'benutzerverwaltung.benutzerBearbeiten',
url_kwargs=dict(id='id'))
You can pass a dict in the column_html_attrs keyword argument when declaring a specific column inside the table class:
class MyTable(Table):
col = Col('Label', column_html_attrs = {'class': 'my-class'})
For more details check this example from the Flask-Table github.

'NoneType' object has no attribute 'objects'

i am getting 'object' has no attribute 'objects'. Here is my code from views.py
def product_list(request, category_slug=None):
category = None
categories = category.objects.all()
products = Product.object.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,'shop/prod/list.html' , {'category':category,
'categories': categories,
'products': products
})
def product_detail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
cart_product_form = CartAddProductForm()
return render(request,
'shop/prod/detail.html',
{'product': product,
'cart_product_form':cart_product_form })
category = None
categories = category.objects.all()
You are setting category to None then immediately setting categories to a property of category, which can only be an error because None in python is the absence of a value.
You can check out more information on the official python docs here:
https://docs.python.org/3/library/constants.html#None

List<String> to String in C#

I made some tables in Entity Framework 5.0 and generated an SQL Server Database from it. The tables are as follows:
Title (TitleId, TitleName, TitlePrice, ISBN...)
Author (AuthorId, FirstName, LastName)
Category (CategoryId, CategoryName)
The relationships between Title and Author and Title and Category are many to many.
Now, here is how I access what categories are assigned to each title and what authors have contributed to a title.
//for authors
var queryAuthList = from au in ebs.Authors
from t in au.Titles
where t.TitleId == TitleId
select new
{
Name = string.Concat(au.FirstName, " ", au.LastName)
};
//for categories
var queryCatList = from cat in ebs.Categories
from t in cat.Titles
where t.TitleId == TitleId
select new
{
cat.CategoryName
};
Now, how do I display the contents of queryAuthList or queryCatList in a simple text label?
I want the output to be like AuthorA, AuthorB, AuthorC for authors and CategoryX, CategoryY, CategoryZ for categories assigned to a single title.
I tried declaring a List<String> and adding the items to the list in a foreach loop, and then printing the list using String.Join(",",strList). However the output appears as
{Name = AuthorA}, {Name = AuthorB}, {Name = AuthorC}
{CategoryName = CategoryX}, {CategoryName = CategoryY}, {CategoryName = CategoryZ}
The {Name = AuthorA} you see is the default ToString() for an anonymous type.
You don't want that, so do not wrap it in a anonymous type:
var queryAuthList = from au in ebs.Authors
from t in au.Titles
where t.TitleId == TitleId
//select new
//{
// Name = string.Concat(au.FirstName, " ", au.LastName)
//};
select string.Concat(au.FirstName, " ", au.LastName);
And since queryAuthList now is an IEnumerable<string>,
string result = String.Join(",", queryAuthList);

Linq2XML missing element

How do I modify the query below to properly handle the case where the "Summary" element is missing from one of the articles? Now when that happens I get an "Object reference not set to an instance of an object."
var articles = from article in xmlDoc.Descendants("Article")
select new {
articleId = article.Attribute("ID").Value,
heading = article.Element("Heading").Value,
summary = article.Element("Summary").Value,
contents = article.Element("Contents").Value,
cats = from cat in article.Elements("Categories")
select new {
category = cat.Element("Category").Value
}
};
The problem is that article.Element("Summary") returns null if the element is not found, so you get a NullReferenceException when you try to get the Value property.
To solve this, note that XElement also has an explicit conversion to string. This won't throw if the XElement is null - you will just get a null string reference.
So to solve your problem you can change this:
summary = article.Element("Summary").Value,
to this:
summary = (string)article.Element("Summary")

Resources