如何从Django中的BinaryField获取字节?

时间:2018-12-29 19:15:47

标签: python django memory hash binary

考虑我们有一个带有BinaryField的模型:

from django.db import models
import hashlib

class Target(models.Model):
    # ...
    image = models.BinaryField(max_length=(1<<24)-1)
    # ...
    def __str__(self):
        return hashlib.md5().update(self.image).hexdigest()

以上代码是否正确计算了图像的MD5摘要? 还是BinaryField内有一些方法或变量来获取传递给update()方法的内存?

更新:当我尝试时:

>>> from pathlib import Path
>>> t = Target(image=Path('../../Experiments/LoginError2.jpg').read_bytes())
>>> t

我遇到以下错误:

AttributeError: 'NoneType' object has no attribute 'hexdigest'

那我在做什么错了?

2 个答案:

答案 0 :(得分:1)

关于import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import classnames from "classnames"; import { Link } from "react-router-dom"; import Moment from "react-moment"; import { addLike, removeLike } from "../../actions/postActions"; class PostedItem extends Component { onLikeClick(id) { this.props.addLike(id); } onUnlikeClick(id) { this.props.removeLike(id); } findUserLike(likes) { const { auth } = this.props; if (likes.filter(like => like.user === auth.user.id).length > 0) { return true; } else { return false; } } render() { const { post, showActions } = this.props; return ( <div className="card mb-3"> <div className="row"> <div className="col-md-3 text-center m-auto px-0 py-3"> <img src={post.avatar} alt={post.designation} /> </div> <div className="col-md-9 m-auto p-0"> <div className="card-body m-2"> <h2 className="card-title"> {post.designation} - {post.location} </h2> <div className="mx-2"> <ul className="list-unstyled"> <li> <b>Date Posted:&nbsp;</b> <Moment format="DD/MM/YYYY">{post.created}</Moment> </li> <li> <b>Salary:&nbsp;</b>₹{post.salary / 100000}&nbsp;lacs/annum </li> <li> <b>Experience Required:&nbsp;</b> {post.experience} years </li> </ul> <p className="card-text">{post.description}</p> </div> </div> <div className="card-footer mx-3 p-2"> <div className="row px-3"> {showActions ? ( <span> <button onClick={this.onLikeClick.bind(this, post._id)} type="button" className="btn btn-light mr-1" > <i className={classnames("fas fa-thumbs-up", { "text-info": this.findUserLike(post.likes) })} /> <span className="badge badge-light"> {post.likes.length} </span> </button> <button onClick={this.onUnlikeClick.bind(this, post._id)} type="button" className="btn btn-light mr-1" > <i className="text-secondary fas fa-thumbs-down" /> </button> </span> ) : null} <Link to={`/post/${post._id}`} className="btn btn-info ml-auto"> View Details </Link> </div> </div> </div> </div> </div> ); } } PostedItem.defaultProps = { showActions: true }; PostedItem.propTypes = { addLike: PropTypes.func.isRequired, removeLike: PropTypes.func.isRequired, post: PropTypes.object.isRequired, auth: PropTypes.object.isRequired }; const mapStateToProps = state => ({ auth: state.auth }); export default connect( mapStateToProps, { addLike, removeLike } )(PostedItem); 的更新,您错误地调用了AttributeError方法。

问题是,hexdigest()方法返回update(),而您几乎要尝试在None上调用hexdigest()。这样的链接方法调用仅在前一个方法返回实际对象而不是None的情况下才有效。

您必须分多个步骤进行此呼叫:

None

答案 1 :(得分:0)

因此,得出的结论是:BinaryField可以分配字节,也可以读取为字节。

在上面的代码中,哈希处理是错误的,正确的方法是:

hashlib.md5(self.image).hexdigest()