Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign Key with Tastypie
    primarykey
    data
    text
    <p>I have a problem like this : implement an election system of many cities .</p> <ul> <li>Each city has at most 3 candidates</li> <li>The city status is : Election Openning</li> </ul> <p>Here is my Models :</p> <pre><code>class City(models.Model): ELECTION_CLOSED = -1 ELECTION_OPENNING = 0 ELECTION_BEGIN = 1 ELECTION_STATUSES = ( (ELECTION_CLOSED,"Election closed"), (ELECTION_OPENNING,"Election Openning"), (ELECTION_BEGIN, "Election Begin"), ) city = models.ForeignKey(Location,related_name='location') president = models.ForeignKey(User,related_name='president',default=None,blank=True,null=False) election_status = models.IntegerField(choices=ELECTION_STATUSES,default=ELECTION_OPENNING) start_electing_at = models.DateTimeField(auto_now_add=True) start_president_at = models.DateTimeField(auto_now_add=True) class Candidate(models.Model): city = models.ForeignKey(Location) candidate = models.ForeignKey(User,related_name='votegame_candidate_id') num_of_votes = models.PositiveIntegerField(default=0) </code></pre> <p>And here is my Resource </p> <pre><code>class CityResource(ModelResource): city = fields.ToOneField(LocationResource, 'city') class Meta: allowed_methods = ['post','get'] queryset = City.objects.all() class CandidateResource(ModelResource): city = fields.ToOneField(LocationResource, 'city') candidate = fields.ToOneField(UserResource, 'candidate') class Meta: allowed_methods = ['post','get'] include_resource_uri = False queryset = Candidate.objects.all() always_return_data = True authentication = BasicAuthentication() authorization= Authorization() filtering = {"city": ALL } def obj_create(self, bundle, request=None, **kwargs): city_res = self.city.get_related_resource(self) print city_res.id **#######I want to check if the City status is "Election Openning" here####** userbalance,created = Balance.objects.get_or_create(user=request.user) obj = self.obj_get_list(request,city=bundle.data.get("city")) **#######I want to check if the number of Candidates in the city is now 3 here ######** try: bundle= super(CandidateResource, self).obj_create(bundle, request, user=request.user) except IntegrityError,e: print e return bundle </code></pre> <p>See my 2 problem above ? I run the POST : </p> <pre><code>curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"city" : "/votegame/v1_votegame/city/123/"}' http://127.0.0.1:8000/votegame/v1_votegame/candidate/ -u summer:123456 </code></pre> <p>Problem 1: </p> #I want to check if the City status is "Election Openning" here <p>I do : <code>print city_res.id</code> , and I got <code>&lt;tastypie.fields.CharField object at 0xb50d7ecc&gt;</code></p> <p>Problem 2 :</p> #I want to check if the number of Candidates in the city is now 3 here <p>I got nothing when <code>print obj</code> But if I change to</p> <pre><code>obj = self.obj_get_list(request,city="123") </code></pre> <p>I got the list.</p> <p>Can anyone pls give me some idea. Thank you very much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload