小编jpr*_*itt的帖子

Django管理员默认自定义字段的顺序

我有以下django admin exceprt:

class TagAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}
    list_display = ['title', 'total_videos', 'total_active_videos', 'weight', 'status']
    search_fields = ['title']
    list_filter = ['status']
    ordering = ['-weight']

    def queryset(self, request):
        return Tag.objects.annotate(total_videos_order=Count('video'))

    def total_videos(self, obj):
        return obj.total_videos_order
    total_videos.admin_order_field = 'total_videos_order'

    def total_active_videos(self, obj):
        return u'%s' % Video.objects.filter(tags=obj, status=Video.STATUS_ACTIVE).count()
Run Code Online (Sandbox Code Playgroud)

现在一切正常,total_videos和total_active_videos都显示正确的信息.如果单击表标题,total_videos也是可排序的.但是,如果我尝试将代码更改为:

ordering = ['total_videos_order']
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

TagAdmin.ordering [0]'指的是模型'videos.Tag'中缺少的字段'total_videos_order'.

如果它只是"total_videos"相同.作为一种解决方法,我可以忽略它,只需点击标题,但它困扰我的原因,如果通过单击默认顺序排序无法设置,当字段显然存在?有什么想法吗?

问题的第二部分是否可以在过滤字段"total_active_videos"上添加排序.根据我的理解,它不可能用条件注释,因此它不能像"total_videos"那样完成,但还有其他解决方案吗?

谢谢!

python django orm admin django-postgresql

5
推荐指数
0
解决办法
1117
查看次数

TextView在水平LinearLayout中拉伸并推动屏幕外的另一个视图

我有一个水平LinearLayout的2项:a TextView和a RatingBar.我希望RatingBar始终直接在右边TextView.但我希望TextView尽可能多地伸展,直到没有空间,然后我想要它添加线条.

以下是它应该如何显示的直观表示,显示不同的文本宽度:

| TextView - RatingView ------------- |

| TextViewTextView - RatingView ----- |

| TextViewTextViewTextVi - RatingView |
| ewTextViewTextView                  |
Run Code Online (Sandbox Code Playgroud)

然而,RatingBar随着TextView的延伸,不断被推离屏幕.这就是它的作用:

| TextView - RatingView ------------- |

| TextViewTextView - RatingView ----- |

| TextViewTextViewTextViewTextViewTex |
| tView                               |
Run Code Online (Sandbox Code Playgroud)

这是XML:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextView"
        android:id="@+id/titleTextView"
        android:textSize="15sp"
        android:textColor="@color/blue"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="8dp"
        android:layout_weight="1"/>

    <RatingBar
        android:layout_width="106dp"
        android:layout_height="20dp"
        android:id="@+id/ratingBar2"
        android:numStars="5"
        android:rating="0"
        style="@style/customRatingBar"
        android:layout_gravity="center_vertical"
        android:isIndicator="true" /> …
Run Code Online (Sandbox Code Playgroud)

android android-layout

5
推荐指数
1
解决办法
1237
查看次数