无法将用户字段内联到UserProfile管理标签 - ' auth.User'没有ForeignKey到' MyApp.UserProfile'

时间:2016-04-11 17:06:50

标签: python django django-models django-admin

我尝试将用户字段添加到用户个人资料管理标签中,以便能够在一个标签中更改每个用户属性。问题是<activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> </activity> 返回:

编辑:根据尚旺的评论编辑代码,但仍然引发错误:

cmd

实际上,我想在UserProfile中使用User或在User中使用UserProfile。问题是UserProfile有<class 'MyApp.admin.UserProfileUserInline'>: (admin.E202) 'auth.User' has no ForeignKey to 'MyApp.UserProfile'. 字段{请注意,这不是语言模型)language我无法弄清楚如何将其内联到用户(UserProfile可以内联没有问题),所以我试图将用户内联到UserProfile。

Admin.py:

through-model

Models.py:

from django.contrib import admin
from models import *
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
admin.site.register(AdminContact)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'User_Profile'
    readonly_fields  = ('languages',)

class UserProfileLanguageLevelInline(admin.TabularInline):
    model = UserProfileLanguage

class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline, )

class UserProfileUserInline(admin.StackedInline):
    model = User

class UserProfileAdmin(admin.ModelAdmin):
    inlines = (UserProfileLanguageLevelInline,User,)

admin.site.unregister(User)
admin.site.register(User,UserAdmin)
admin.site.register(LanguageTuple)
admin.site.register(Language)
admin.site.register(Job)
admin.site.register(UserProfileLanguage)
admin.site.register(Level)
admin.site.register(UserProfile,UserProfileAdmin)

1 个答案:

答案 0 :(得分:0)

User inlines中的UserProfileAdmin模型不应该User,因为class UserProfileAdmin(admin.ModelAdmin): inlines = (UserProfileLanguageLevelInline, UserProfileUserInline) 是一个模型,而不是内联管理员。像这样编辑:

   public class DisplayStudentNames extends AppCompatActivity {
   String myJSON;
   private static final String TAG_RESULTS = "result";
   private static final String TAG_ROLL = "RollNo";
   private static final String TAG_NAME = "Name";
   JSONArray peoples = null;
   ListView list;
   ArrayList<String> checkedValue;
   Button b1;
   MyCustomAdapter dataAdapter = null;
   ArrayList<Student> personList=null;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_display_student_names);
   final Spinner sbranch = (Spinner) findViewById(R.id.branch);
   final Spinner ssemester = (Spinner) findViewById(R.id.semester);
   final Spinner ssubject = (Spinner) findViewById(R.id.edit_subject);
   //String branch=sbranch.getSelectedItem().toString();
   //String semester=ssemester.getSelectedItem().toString();
   //String subject=ssubject.getSelectedItem().toString();
   String branch = "cs";
   String semester = "7";
   String subject = "Soft Computing";
   b1 = (Button) findViewById(R.id.submit);
   list = (ListView) findViewById(R.id.listView);

   new DataFetch().execute(branch, semester, subject);
   list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Student student = (Student) parent.getItemAtPosition(position);
        String Name = student.getName();
        //String s=(String) ((TextView) view.findViewById(R.id.roll)).getText();
        Toast.makeText(getApplicationContext(), "Clicked: " + Name, Toast.LENGTH_SHORT).show();

    }
});
//submitAttendance();
}

class DataFetch extends AsyncTask<String, String, String> {
ArrayList<NameValuePair> params;
private ProgressDialog pDialog;
private String url_fetch_data = "http://192.168.1.4/AttendanceApp/fetch_data.php";
InputStream is = null;
String res = "";

protected void onPreExecute() {
    super.onPreExecute();
}

protected String doInBackground(String... args) {
    String branch = args[0];

    String semester = args[1];
    String subject1 = args[2];

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_fetch_data);
        params = new ArrayList<NameValuePair>(3);
        params.add(new BasicNameValuePair("branch", branch));
        params.add(new BasicNameValuePair("semester", semester));
        params.add(new BasicNameValuePair("subject", subject1));
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);

        StringBuilder total = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            total.append(line);
        }
        String result = total.toString();
        return result;
    } catch (Exception e1) {
        res = "error:" + e1.getMessage().toString();
        e1.printStackTrace();
    }
    return res;
}

protected void onPostExecute(String httpResponse) {
    myJSON = httpResponse;
    showList();
}
}

protected void showList() {
try {
    JSONObject jsonObj = new JSONObject(myJSON);
    peoples = jsonObj.getJSONArray("result");

    personList = new ArrayList<Student>();

    for (int i = 0; i < peoples.length(); i++) {
        Student student = new Student();
        JSONObject c = peoples.getJSONObject(i);
        String roll = c.getString(TAG_ROLL);
        String name = c.getString(TAG_NAME);
        student.setName(name);
        student.setRollNo(roll);
        personList.add(student);
    }

    dataAdapter = new MyCustomAdapter(this, R.layout.list_item, personList);
    list.setAdapter(dataAdapter);



} catch (JSONException e) {
    e.printStackTrace();
}
}