Wednesday, August 8, 2012

Many ways of making Queries with foreign keys in Django

1)

    operator_id = crud_instance.operator_id
    operator = crud_instance.operator




    productset = Product.objects.filter(operator=operator)
    productset = Product.objects.filter(operator_id=operator_id)



Both these lines are usable.
=operator_id is the actual Fk field in the db table. Lookup with unicode/int operator_id.
=operator looks up with an instance of operator model object. Same effect.

2)
3)
4) user.user.groups (UserProfile table) : mapping fk User to fk Operator
User = auth_user
auth_user table does not have Fk to groups table.
But you can try to use user.groups, it will refer to table auth_user_groups MAGICALLY.


    user_set = UserProfile.objects.filter(operator_id=operator_id)
    for user in user_set:
        group_name = user.user.group.name
        print group_name #get_user_group_cfg(user.user) #user.user.groups
        if group_name == "Operator Admin": admin_ok = True
        if group_name == "Operator Webservice": webservice_ok = True
        #request_logger.warning("Duplicate entry in auth_user_groups: 1 user with 2 groups.")
        if admin_ok and webservice_ok:
            return True

    return False



5)

    def handle_request(self, request, *args, **kwargs):
        """ubill temp

        operator edit: you have the operator_id from url
        self.operator_id, self.operator
        """
        super(OperatorEditCrud, self).handle_request(request, *args, **kwargs)

        #zero_product_check(crud_instance=self)
        print "pass?", minimum_users_check(crud_instance=self)
        print "===Handled request==="






6)
View code to trigger your own functions


    def handle_request(self, request, *args, **kwargs):
        """ubill temp

        operator edit: you have the operator_id from url
        self.operator_id, self.operator
        """
        super(OperatorEditCrud, self).handle_request(request, *args, **kwargs)

        #zero_product_check(crud_instance=self)
        print "pass?", minimum_users_check(crud_instance=self)
        print "===Handled request==="





















No comments:

Post a Comment