我有一个Recipe模型,它包含一个IngredientType对象的M2M字段.这个字段,也就是ingredient_list,通过一个Ingredient对象使用臭名昭着的"通过"模型,该对象将额外的数据添加到我的IngredientType中.这些是我的课程:
class Recipe(models.Model):
user_profile = models.ForeignKey(UserProfile, null=True, blank = True)
name = models.CharField(max_length=200)
photo1 = models.ImageField( upload_to = 'images/recipies', help_text="This photo will show by default")
ingredient_list = models.ManyToManyField(IngredientType,through='Ingredient')
class Ingredient(models.Model):
ingredient_type = models.ForeignKey(IngredientType)
recipe = models.ForeignKey(Recipe)
amount = models.IntegerField()
units = models.CharField(max_length=4,choices=UNIT,
default=None, null=True, blank = True)
class IngredientType(models.Model):
name = models.CharField(max_length=200)
plural_name = models.CharField(max_length=200)
photo = models.ImageField( upload_to = 'images/ingredients')
is_main = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)
我尝试使用rest_framework序列化它们:
class IngredientTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = IngredientType
fields=('name', 'plural_name', 'photo', 'is_main')
class IngredientSerializer(serializers.HyperlinkedModelSerializer):
ingredient_type = …Run Code Online (Sandbox Code Playgroud) 我一直在使用viewPager遇到麻烦我正试图添加到我的应用程序中.
我有一个SearchResultActivity,里面有一个抽屉.具有以下布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
Run Code Online (Sandbox Code Playgroud)
因为我正在使用片段,在第一次到达此活动时,我打开使用事务在Activity的onCreate方法中打开RecipeHeadlinesFragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
mSearchType = getIntent().getIntExtra(SEARCH_TYPE, -1);
if (mSearchType == ICON_SEARCH) {
mVeggieNamesIcons = getIntent().getStringArrayListExtra(
VEGGIE_NAME_ARR);
mVeggieUrlsIcons = getIntent().getStringArrayListExtra(
VEGGIE_URLS_ARR);
} else if (mSearchType == QUERY_SEARCH) {
mQuery = getIntent().getStringExtra(QUERY);
}
// Generate title
title = new String[] { "Search for a recipe", "My profile",
"Title Fragment 3" };
// Generate …Run Code Online (Sandbox Code Playgroud) java android illegalstateexception android-fragments android-viewpager