使用用户ID过滤用户详细信息

时间:2018-11-12 07:24:44

标签: django django-rest-framework

当我从前端传递用户ID时,我需要从Django rest模型中获取所有用户详细信息到相应的视图。

class UserDetails(viewsets.ModelViewSet):
    def get_queryset(self):
        user_id = self.request.query_params.get('user_id',None)
        //*queryset = UserModel.objects.all()*
        //instead of getting all the objects from UserModel, I need to get the particular user_id object only
        return querysest

2 个答案:

答案 0 :(得分:3)

您需要尝试从 URL 获取user_id参数。

然后尝试根据获得的user_id查询集中进行过滤。

def get_queryset(self):
    # Get queryset of User Model
    queryset = UserModel.objects.all()

    # Try to fetch the user_id param from url
    user_id = self.request.query_params.get('user_id', None)

    # If user_id param is not None, filter using the obtained user_id
    if user_id is not None:
        queryset = queryset.filter(id=user_id)

    return queryset

答案 1 :(得分:1)

也许是这样的:

class CommentsCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UIDocumentInteractionControllerDelegate {

var dic = UIDocumentInteractionController()
var imgCollection: [TicketAttachment] = [TicketAttachment]()
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var imgProfilePic: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblDate: UILabel!
@IBOutlet weak var txvComments: UITextView!
override func awakeFromNib() {
    super.awakeFromNib()
    dic.delegate = self
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    self.collectionView.register(UINib.init(nibName: "AttachmentViewCell", bundle: nil), forCellWithReuseIdentifier: "AttachmentViewCell")
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let fileUrl = imgCollection[indexPath.row].fileUrl?.absoluteString
        let url = URL(string: Api.domain + fileUrl!)

        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

        sharedAFManager.AFManager.download(url!, to: destination)
        .downloadProgress(closure: { _ in
            SVProgressHUD.show()
        }).response(completionHandler: { (downloadResponse) in
            SVProgressHUD.dismiss()
            self.dic.url = downloadResponse.destinationURL
            self.dic.uti = downloadResponse.destinationURL!.uti
            let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
            self.dic.presentOpenInMenu(from: rect, in: self.view, animated: true)
        })
}