whoisearth
whoisearth

Reputation: 4170

django - running query filter by user

I'm having trouble trying to figure out how to run a query after logging in as a user while using the user id to decide what should return in the queryset. In doing the below all I get as a return is -

'str' object is not callable

I'm using mezzanine on the basic setup right now with an install using a set account that can view some results on the UserFile model but not all.

From an SQL syntax what I'm trying to do is the following -

select * from UserFile where userid = (user that's logged in)

here is my views.py -

class Vendor_Matrix(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    serializer_class = User_Serializer
    permission_classes = (User, permissions.IsAuthenticated)

    def get(self, request, *args, **kwargs):
        user = request.user
        queryset = UserFile.objects.filter(username=request.user).values_list('vendorid', 'fileid', 'grant_date', 'revoke_date')
        return self.retrieve(request, *args, **kwargs)

And my models.py

class UserFile(models.Model):
    userid = models.ForeignKey(User, db_column='userid')
    fileid = models.ForeignKey(FileIndex, db_column='id')
    grant_date = models.DateTimeField()
    revoke_date = models.DateTimeField()
    class Meta:
        db_table = 'auth_files'
        verbose_name = 'User File Matrix'
        verbose_name_plural = 'User File Matricies'

I'm calling it all through the following -

urls.py

urlpatterns = patterns('',
    url(r'^your-data/vendor-matrix/$', 'vendor_matrix'),
)

Upvotes: 0

Views: 110

Answers (2)

whoisearth
whoisearth

Reputation: 4170

I ended up getting it to work by focusing on stripping down my view to the bare bones and building it up again. This worked -

def index(request):
    userid = None
    if request.user.is_authenticated():
        userid = request.user.id
    queryset = UserFile.objects.filter(userid=userid).values_list('fileid', 'grant_date', 'revoke_date')
    return JSONResponse(queryset)

Upvotes: 0

gaw
gaw

Reputation: 1960

As I understand this you try to filter on username=.... where there is no field username in your UserFile model additionally the vendorid is missing as well?

The error might be resulting from the fact that Django can't resolve the 'vendorid' string since there is no corresponding field and thatswhy states that this str is not callable.

Might be helpful to see the complete stack and in case that the Model is only partly documented to have the complete Model.

Upvotes: 1

Related Questions