我是Django的新手(已使用1.11),我读了这篇文章(https://docs.djangoproject.com/fr/1.11/topics/db/models/),但我没有找到一个明确的答案:如何检索对象
举一个例子,假设我有以下模型:
#include <iostream>
#include <string>
#include <QuartzCore/QuartzCore.h>
#include <CoreServices/CoreServices.h>
#include <ImageIO/ImageIO.h>
int main(int argc, const char * argv[]) {
std::string baseImageOutput = "/Users/bogdan/Desktop";
std::string pathSeparator = "/";
std::string baseImageName = "image-";
std::string imageExtension = ".png";
CGDisplayCount displayCount;
CGDirectDisplayID displays[32];
// grab the active displays
CGGetActiveDisplayList(32, displays, &displayCount);
// go through the list
for (int i = 0; i < displayCount; i++) {
std::string imagePath = baseImageOutput + pathSeparator + baseImageName + std::to_string(i) + imageExtension;
const char *charPath = imagePath.c_str();
CFStringRef imageOutputPath = CFStringCreateWithCString(kCFAllocatorDefault, charPath, kCFURLPOSIXPathStyle);
// make a snapshot of the current display
CGImageRef image = CGDisplayCreateImage(displays[i]);
CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault, imageOutputPath, NULL);
// The following CGImageDestinationRef variable is NULL
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
if (!destination) {
std::cout<< "The destination does not exist: " << imagePath << std::endl;
CGImageRelease(image);
return 1;
}
CGImageDestinationAddImage(destination, image, NULL);
if (!CGImageDestinationFinalize(destination)) {
std::cout << "Failed to write image to the path" << std::endl;;
CFRelease(destination);
CGImageRelease(image);
return 1;
}
CFRelease(destination);
CGImageRelease(image);
}
std::cout << "It Worked. Check your desktop" << std::endl;;
return 0;
}
技能历史:
class StudentCollaborator(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
code_postal = models.IntegerField()
collaborative_tool = models.BooleanField(default=False)
def get_skills(self):
return SkillHistory.objects.filter(student=self.user, value="acquired").values_list('skill')
最后是技能课程:
class SkillHistory(models.Model):
"""
The reason why a Skill is acquired or not,
or not yet, when and by who/how
"""
skill = models.ForeignKey(Skill)
"""The Skill to validate"""
student = models.ForeignKey('users.Student')
"""The Student concerned by this Skill"""
datetime = models.DateTimeField(auto_now_add=True)
"""The date the Skill status was created"""
value = models.CharField(max_length=255, choices=(
('unknown', 'Inconnu'),
('acquired', 'Acquise'),
('not acquired', 'None Acquise'),
))
"""The Skill status : unknown, acquired or not acquired"""
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
reason_object = GenericForeignKey('content_type', 'object_id')
reason = models.CharField(max_length=255)
"""Why the Skill is validated or not"""
by_who = models.ForeignKey(User)
class Meta:
ordering = ['datetime']
如何在get_skills函数中检索Skill对象(而不是ID);谢谢?
由于
答案 0 :(得分:4)
您无法直接获取Skill对象。但您可以通过此查询获取技能属性。
SkillHistory.objects.filter(student=self.user, value="acquired").values('skill__name', 'skill__code')
答案 1 :(得分:1)
Skill.objects.filter(skillhistory_set__student=self.user, skillhistory_set__value="acquired")
使用相关名称过滤。