小编ben*_*eni的帖子

查询Django中的子查询

我正在尝试从另一个查询执行查询,但Django说:'渲染时捕获了DatabaseError:子查询返回的行数超过1行.我正在使用PostGis.

我的模特

class Place(models.Model):    
    coordinate = models.PointField()

class TranslatedPlace(models.Model):
    place = models.ForeignKey(Place)
Run Code Online (Sandbox Code Playgroud)

我的看法

  near_coordinates = Place.objects.filter(coordinate__distance_lte=(place_obj.coordinate, D(km=100)))
  near_places = TranslatedPlace.objects.filter(place=near_coordinates)
Run Code Online (Sandbox Code Playgroud)

django django-models django-views

3
推荐指数
1
解决办法
1848
查看次数

'dict'对象没有属性朋友

我收到此错误:渲染时捕获AttributeError:'dict'对象没有属性'友谊'.问题是当我尝试在自定义模板标记中获取friend.friend的值时.模块'friends_for_user'是对的.我有:

楷模

class FriendshipManager(models.Manager):

  def friends_for_user(self, user):
     friends = []
     for friendship in self.filter(from_user=user).select_related(depth=1):
        friends.append({"friend": friendship.to_user, "friendship": friendship})
     for friendship in self.filter(to_user=user).select_related(depth=1):
        friends.append({"friend": friendship.from_user, "friendship": friendship})
     return friends
Run Code Online (Sandbox Code Playgroud)

模板标签

 def render(self, context):
    user = self.user.resolve(context)
    num = self.num.resolve(context)

    my_friends = Friendship.objects.friends_for_user(user)

    potential_friends = []
    for friend in my_friends:
        potential_friends.append(Friendship.objects.friends_for_user(friend.friend))  //This line is the error.

    context[self.context_name] = potential_friends
    return ''
Run Code Online (Sandbox Code Playgroud)

django django-templates django-models

3
推荐指数
1
解决办法
3427
查看次数

如何将属性'_meta'添加到对象?

我正在尝试将django-voting应用程序添加到我的项目中.我不知道如何在我的模板中使用它,所以当用户点击按钮时,我会添加一个新的模板标签用于向上或向下投票.我不知道是否有良好的形式来做到这一点.

我的问题是模板标签中的这些行:

obj = Place.objects.filter(id=object_id)
Vote.objects.record_vote(obj, self.user, +1)
Run Code Online (Sandbox Code Playgroud)

django打印:

Caught AttributeError while rendering: 'Place' object has no attribute '_meta'
Run Code Online (Sandbox Code Playgroud)

如何添加属性_meta我的对象'Place'?

django django-templates django-models django-voting

2
推荐指数
1
解决办法
7443
查看次数

下载图像表单Web时,BitmapFactory.decodeStream返回null

我正在尝试使用Google示例页面从网址下载图片.我在BitmapFactory.decodeStream方法中使用InputStream时读过,我不能使用两次.我试图这样做,但它不起作用'因为它在解码图像中返回null,我不知道我能做什么.

这是我的代码:

这部分是在AsyncTask类中的doInBackground方法中

Bitmap bitmapImage;
URL imageUrl = null;
try {
imageUrl = new URL(url[0]);

HttpGet httpRequest = null;
httpRequest = new HttpGet(imageUrl.toURI());

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();

    bitmapImage = CommonMethods.decodeSampledBitmapFromResource(instream, thumb_width, thumb_width);

instream.close();
return bitmapImage;

 } catch (URISyntaxException e) {
    e.printStackTrace();
    return null;
 } catch (MalformedURLException e) {
    e.printStackTrace();
    return null;
 } catch (IOException e) {
    e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

android bitmap bitmapfactory

2
推荐指数
1
解决办法
1万
查看次数