如何定义返回相关对象的方法

时间:2015-05-13 16:52:25

标签: python django

我有两种模式:

public class Contactmedia extends ListActivity {

    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();

    private static final String READ_CONTACT_URL = "http://www.iamnotcrazy.hol.es/webservice/contact.php";

    private static final String TAG_NUMBER ="number";
    private static final String TAG_POSTS = "posts";

    private JSONArray mid = null;
    //manages all of our comments in a list.
    private ArrayList<HashMap<String, String>> mContactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.contactmedialist);   
    }

    @Override
    protected void onResume() {

        super.onResume();

        new LoadComments().execute();
    }

    /**
     * Retrieves json data of comments
     */
    public void updateJSONdata() {
        mContactList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(READ_CONTACT_URL);

        try { 

            mid= json.getJSONArray(TAG_POSTS);

            for (int i = 0; i < mid.length(); i++) {
                JSONObject c = mid.getJSONObject(i);            
                String number = c.getString(TAG_NUMBER);
                HashMap<String, String> map = new HashMap<String, String>();                 
                map.put(TAG_NUMBER, number);  
                mContactList.add(map);     
            }

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

    /**
     * Inserts the parsed data into our listview
     */
    private void updateList() {

        ListAdapter adapter = new SimpleAdapter(this, mContactList,
                R.layout.contactmediadesign, new String[] { TAG_NUMBER
                         }, new int[] { R.id.contactno 
                        });

        setListAdapter(adapter);

        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                /* this is where i have problem how to get  that number without converting to string*/
                TextView v = (TextView)view.findViewById(R.id.contactno); 
                int myNum = Integer.parseInt(v.getText().toString());
                /* and here i want use that mynum after getiing phonenumber for calling purpouse ass shown below 
                 * but its not working :(*/
                if (position == 0){
                    Toast.makeText(getApplicationContext(), "yes you done it!!", Toast.LENGTH_SHORT).show();
                    Intent callIntent = new Intent(Intent.ACTION_CALL);  
                    callIntent.setData(Uri.parse("tel:"+"myNum"));  
                    startActivity(callIntent);
                }

            }
        });
    }       

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Contactmedia.this);
            pDialog.setMessage("Loading complaints...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected Boolean doInBackground(Void... arg0) {

              updateJSONdata();

            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();

            updateList();
        }
    }
}

我希望通过个人资料班访问宠物。当我在终端上呼叫class Profile(models.Model): user = models.OneToOneField(User, null=True) address = models.CharField(max_length=500) def pets(self): return Pet.objects.filter(owner=self.id) class Pet(models.Model): owner = models.ForeignKey(Profile) name = models.CharField(max_length=150) 时,响应是:

profile.pets

如何为配置文件对象获取宠物?

2 个答案:

答案 0 :(得分:1)

profile = Profile.objects.get(id=id_of_profile_you_want)
list_of_pets = profile.pet_set

答案 1 :(得分:0)

profile.pets是一种方法。

你必须把它叫做

pets = profile.pets()