I have the following code using MongoEngine:
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
class Post(Document):
title = StringField(max_length=120, required=True)
author = StringField(required=True)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
# Create a post:
post = Post(title="Quora rocks", author="Ross", tags=['tutorial', 'how-to'])
post.save()
comment1 = Comment(content="Great post!", name="john")
comment2 = Comment(content="Great post too!", name="dave")
post.comments.append(comment1)
post.comments.append(comment2)
post.save()
# Create a post:
post = Post(title="Books", author="Chandler")
post.save()
comment1 = Comment(content="Stupid post!", name="justin")
comment2 = Comment(content="Stupid post!", name="mark")
post.comments.append(comment1)
post.comments.append(comment2)
post.save()
I want to first get the post from Ross and then find the comment from john. What is the best way to do it ? I have MongoEngine 0.9.5
I think I figured out the problem. We need to EmbeddedDocumentListField instead of ListField of EmbeddedDocumentField. This makes filtering the embedded document much easier.
Related
I am slowly progressing in my django journey, but this one has me stumped. I am trying to populate a CreateView with a different model via a copy command using the get_initial override. All of the attributes copy as I would expect with the exception of the ManytoMany fields. I've researched this topic most of today, and found the following which is very close to what I'm trying to figure out KeyError: 'manager' in django get_initial.
My View...
class BookView(LoginRequiredMixin,CreateView):
model = Book
template_name = 'book/titles.html'
form_class = BookForm
def get_initial(self):
initial = super(BookView, self).get_initial()
author = author.objects.get(pk=self.kwargs["pk"])
initial = author.__dict__.copy()
initial.update({
"author": author.name,
}}
for field in self.form_class.base_fields.items():
value = getattr(self.get_object(), field)
if field == 'author':
value = self.get_object().author.all()
initial.update({field: value})
return initial
I incorporated the suggested change based on the issue that I found on SO, but I still am getting a 'manager" KeyError. I am ultimately trying to populate the manytomanyfield in my model and then save the values, but to no avail. Any suggests are appreciated!
What a difference a day makes....
def get_initial(self):
initial = super(BookView, self).get_initial()
author = author.objects.get(pk=self.kwargs["pk"])
initial = author.__dict__.copy()
initial.update({
"author": author.name.all(),
}}
return initial
I added a .all() after the reference to the manytomanyfield in my initial get and also update the form to get the field in question. Much cleaner than a few hacks I kinda got working along the way.
I'm trying to find out where the MOdule Feauture Item Slider in Orchard stored it's ImgUrl, I don't see any field that store its image in the table.
.Select(fi => new FeaturedItemViewModel {
Headline = fi.Headline,
SubHeadline = fi.SubHeadline,
LinkUrl = fi.LinkUrl,
SeparateLink = fi.SeparateLink,
LinkText = fi.LinkText,
ImagePath = fi.Fields.Single(f => f.Name == "Picture").Storage.Get<string>(""),
SlideNumber = ++slideNumber
}).ToList();
What is this field?
ImagePath = fi.Fields.Single(f => f.Name == "Picture").Storage.Get("")
I can't find any Name Field in the FeaturedItemPartRecord table
Thanks,
Samuel
That would be an orchard field so it is stored in the Data column of the Orchard_Framework_ContentItemVersionRecord table. It is part of the XML document. Sebastien Ros has a decent blog post explaining orchard's database structure http://sebastienros.com/understanding-orchard-s-database
I think I found it, it's Projections_StringFieldIndexRecord
Thank you
If I have a document like:
class Page(Document):
title = StringField(max_length=200, required=True)
date_modified = DateTimeField(default=datetime.datetime.now)
and I want to update it to:
class Page(Document):
page_title = StringField(max_length=200, required=True)
date_modified = DateTimeField(default=datetime.datetime.now)
what is the best way to handle the database migration in mongoengine? I would imagine you could iterate through the database and pull the objects that have that field and add them back in with the new field then drop all objects that have that field, but it'd be nice if there was an idiomatic way to handle this sort of thing.
I think the easiest way to do it would be by a two-step change of the class.
First add the new field and remove the "required" constraint:
class Page(Document):
title = StringField(max_length=200)
page_title = String(max_length=200)
date_modified = DateTimeField(default=datetime.datetime.now)
Then run the following commands:
for page in Page.objects():
page.page_title = page.title
page.save()
Page.objects.update(unset__title=1) ## Unset the title field of the documents
That will update the documents of your DB without having to drop them (which is useful because they will keep the same id).
Then you make the final modification to your class, as you wanted:
class Page(Document):
page_title = StringField(max_length=200, required=True)
date_modified = DateTimeField(default=datetime.datetime.now)
I am using the following SSJS:
var author = #Author().toString();
var str = author.replace("CN=","");
var str2 = str.replace("O=","");
var str3 = str2.replace("[","");
var str4 = str3.replace("]","");
if("" == str4)
return #Name("[CN]",session.getEffectiveUserName());
else
return var4;
not the perfect way to do it, but...
Question: Why do I get all the users which edited the document in this field? I only want to show the Author of the document.
You get a list of all users because that's what #Author() returns. Here is the description of the function taken from the online help: "Returns the names of the authors of the current document."
You can use #Subset() to get the latest author:
#Subset(#Author(), -1)
and to get the original author:
#Subset(#Author(), 1)
Use #Name() to format the name. So to only show the common name part of the original author, do this:
#Name("[CN]",#Subset(#Author(), 1))
I have two tables:
NewsRooms ( NrID[int] , NrName [string]);
RawNews( RnID [int], NrID[string]);
realtion is RawNews 1 * NewsRooms
so i use checkboxes for NewsRooms and save the ids as a string in RawNews like this ';1;2;'
now for example i have a list which contains some NrIDs . i want to select every RawNew which it's NrID contains any of the ids inside that list.
here is my code:
var temp = Util.GetAvailibleNewsRooms("ViewRawNews");
List<string> ids = new List<string>();
foreach (var item in temp)
ids.Add(";" + item.NrID.ToString() + ";");
model = db.RawNews.Where(r => r.NrID.Any(ids));
the line model = db.RawNews.Where(r => r.NrID.Any(ids)); is wrong and i don't know how to write this code. please guide me. thanks
ok guys, i found the solution myself so i post it here maybe some other guy need it some day!!
model = model.Where(r => ids.Any(i => r.NrID.Contains(i)));