我刚开始使用django查询语言,但遇到了一些困难。我希望方法 get_present_users()使用“Presence”对象返回连接到某个Drink对象的所有用户。我知道我可以通过以下方式获取所有相关的状态对象:
Presence.objects.filter(event=self.pk)
但是,我无法想到将此查询集转换为所有用户的方法。你能帮助我一个人吗?
class Drink(BaseModel):
date = models.DateField()
comments = models.TextField(blank=True)
class Presence(BaseModel):
event = models.ForeignKey(Drink)
submitted_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='presence_submitted_by')
present_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='presence_present_user')
答案 0 :(得分:0)
如果您正在尝试编写一个函数,该函数可以获取Users
对象Drink
与Presence
Presence
关联的所有Users
而不是试图去来自from django.contrib.auth.models import User
...
class Drink(model.Models):
...
def get_present_users(self):
return User.objects.filter(presence_present_user__event=self)
...
并获取drink = Drink.objects.get(id=1)
print(drink.get_present_users())
>> [<User: >, <User: >]
的列表。
@interface MyView : UIView
@end
@implementation MyView
- (instancetype)init
{
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientationHandler:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}
- (void)updateTransformWithOrientation:(UIInterfaceOrientation)orientation
{
CGFloat width = CGRectGetWidth(self.window.bounds);
CGFloat height = CGRectGetHeight(self.window.bounds);
if (width > height) {
CGFloat temp = width;
width = height;
height = temp;
}
CGFloat offset = (height - width) / 2;
CGAffineTransform transform;
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
transform = CGAffineTransformMakeTranslation(-offset, offset);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIInterfaceOrientationLandscapeRight:
transform = CGAffineTransformMakeTranslation(-offset, offset);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIInterfaceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(-M_PI);
break;
default:
transform = CGAffineTransformIdentity;
break;
}
self.transform = transform;
self.frame = CGRectMake(0, 0, width, height);
}
- (void)updateFrameWithOrientation:(UIInterfaceOrientation)orientation
{
CGFloat width = CGRectGetWidth(self.window.bounds);
CGFloat height = CGRectGetHeight(self.window.bounds);
if (width > height) {
CGFloat temp = width;
width = height;
height = temp;
}
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
self.frame = CGRectMake(0, 0, height, width);
break;
default:
self.frame = CGRectMake(0, 0, width, height);
break;
}
}
- (void)updateWithOrientation:(UIInterfaceOrientation)orientation
{
BOOL isIos7 = [[UIDevice currentDevice].systemVersion floatValue] < 8.0;
BOOL isKeyboardWindow = [self.window isKindOfClass:NSClassFromString(@"UITextEffectsWindow")];
if (isIos7 == YES && isKeyboardWindow == NO) {
[self updateTransformWithOrientation:orientation];
} else {
[self updateFrameWithOrientation:orientation];
}
}
- (void)changeOrientationHandler:(NSNotification *)notification
{
[UIView animateWithDuration:0.25 animations:^{
UIInterfaceOrientation orientation = (UIInterfaceOrientation)[notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
[self updateWithOrientation:orientation];
}];
}
@end
这将允许你写:
dplyr