push item in listfield in django models - django-models

I have a Django model(CategoryModels) and in it, I have a list field as below:
category=['a', 'b'], id_name='test'
and I want to push category 'c' to the id_name='test'
can someone help me?

I add "c" to category list by using:
inst = CategoryModels.objects.get(id_name="test")
inst.category.append("c")
inst.save()

Related

How can I create a database in flutter to insert List items when tapped on?

Hello I have a class of Fish and within this class I create a list of items. Now I'm trying to create a database using Sqflite where if the user tap on an item, the selected item will be added to the database. And finally I want to display the selected items to the user. Please Can you guys save me???
code sample:
class Fish {
List listOfFish = [
"A l'amiral",
"Ackee and saltfish",
"Acqua pazza",
"Agujjim",
"Amplang",
"Arsik",
"Asam pedas",
"Aseed",
// B
"Bacalaito",
"Bacalhau a Bras",
"Bacalhau a Gomes de Sa",
];
}

Create snippet with link to document

I create a custom models.Model and register to snippet.
I wish that this model have a field to a Wagtail document.
How can I do that ?
#register_snippet
class Product(Model):
name = CharField(max_length=255)
bill = ReferenceToWagtailDocument() <-- how to code that
Ideally, I wish that bill can be selectable in Wagtail Snippet.
Any idea to do that ?
Best regards.
I have found a solution by adding a ForeignKey like the following.
#register_snippet
class Product(Model):
name = CharField(max_length=255)
bill = ForeignKey(Document, on_delete=CASCADE, null=True)
Hope that I can help somebody.
One drawback, I don't have the file selector.
Best regards.

Linked List model in Google App Engine Java

I am a beginner in Java and the Google App Engine (java).
I am trying to make a linked list type of model to link up entities together.
For example, I would like Class Fruit to be able to hold or take in a list of fruits, like Apple, Oranges...etc.
I was thinking of using the entity key as pointers between Class Fruit, and class Apple or class Orange.
Is there a way to grab an entity's key, and place it into another entity?
Better yet, grab multiple entity keys and place it into one entity?
Take a look at Objectify - https://code.google.com/p/objectify-appengine/ they have a really nice wrapper on top of the datastore that handles doing this for you.
You can also do this with Mungo-Appengine which has a MongoDB-like syntax with zero-configuration:
Mungo mungo = new Mungo();
DB fruitsDB = mungo.getDB("fruitsDB");
DBCollection fruits = fruitsDB.createCollection("fruits");
DBObject apple = new BasicDBObject("name", "apple")
.put("color", "red");
DBObject orange = new BasicDBObject("name", "orange")
.put("color", "orange")
.put("weight", 1);
DBObject myBasket = new BasicDBObject("name", "myBasket")
.put("fruits", Lists.newArrayList(apple, orange)); // Google Guava
fruits.insert(myBasket); // Done!
DBObject theBasket = fruits.findOne("{'name' : 'myBasket'}"); // Get it
List<DBObject> theFruits = theBasket.get("fruits");
Hope this helps.

Django - sorting a list of results from two QuerySet objects

For example, let's assume that i have two QuerySEt objects:
queryset1 = my_model1.objects.all().order_by('-created')
queryset2 = my_model2.objects.all().order_by('-created')
And i want one list with elements from both querysets, list oreder by '-created' field. Has someone have a recipe for this?
newlist = sorted(itertools.chain(queryset1, queryset2),
key=operator.attrgetter('created'), reverse=True)

AppEngine Datastore get entities that have ALL items in list property

I want to implement some kind of tagging functionality to my app. I want to do something like...
class Item(db.Model):
name = db.StringProperty()
tags = db.ListProperty(str)
Suppose I get a search that have 2 or more tags. Eg. "restaurant" and "mexican".
Now, I want to get Items that have ALL, in this case 2, given tags.
How do I do that? Or is there a better way to implement what I want?
I believe you want tags to be stored as 'db.ListProperty(db.Category)' and then query them with something like:
return db.Query(Item)\
.filter('tags = ', expected_tag1)\
.filter('tags = ', expected_tag2)\
.order('name')\
.fetch(256)
(Unfortunately I can't find any good documentation for the db.Category type. So I cannot definitively say this is the right way to go.) Also note, that in order to create a db.Category you need to use:
new_item.tags.append(db.Category(unicode(new_tag_text)))
use db.ListProperty(db.Key) instead,which stores a list of entity's keys.
models:
class Profile(db.Model):
data_list=db.ListProperty(db.Key)
class Data(db.Model):
name=db.StringProperty()
views:
prof=Profile()
data=Data.gql("")#The Data entities you want to fetch
for data in data:
prof.data_list.append(data)
/// Here data_list stores the keys of Data entity
Data.get(prof.data_list) will get all the Data entities whose key are in the data_list attribute

Resources