我的应用程序的数据库已填充并与外部数据源保持同步。我有一个抽象模型,我的 Django 2.2 应用程序的所有模型都来自该模型,定义如下:
class CommonModel(models.Model):
# Auto-generated by Django, but included in this example for clarity.
# id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
ORIGIN_SOURCEA = '1'
ORIGIN_SOURCEB = '2'
ORIGIN_CHOICES = [
(ORIGIN_SOURCEA, 'Source A'),
(ORIGIN_SOURCEB, 'Source B'),
]
object_origin = models.IntegerField(choices=ORIGIN_CHOICES)
object_id = models.IntegerField()
class A(CommonModel):
some_stuff = models.CharField()
class B(CommonModel):
other_stuff = models.IntegerField()
to_a_fk = models.ForeignKey("myapp.A", on_delete=models.CASCADE)
class C(CommonModel):
more_stuff = models.CharField()
b_m2m = models.ManyToManyField("myapp.B")
Run Code Online (Sandbox Code Playgroud)
该object_id字段不能设置为唯一的,因为我在我的应用程序中使用的每个数据源可能都有一个带有object_id = 1. 因此需要通过场来追踪对象的起源object_origin。 …
给定 Django 2.2 应用程序中的以下模型:
class ShelfPosition(models.Model):
shelf_code = models.CharField(max_length=10)
row = models.IntegerField()
column = models.IntegerField()
class Meta:
constraints = [
models.UniqueConstraint(fields=["shelf_number", "row", "column"], name="shelfpos_unique")
]
class Item(models.Model):
name = models.CharField(max_length=255)
position = models.OneToOneField(to=ShelfPosition, on_delete=models.SET_NULL, primary_key=True)
Run Code Online (Sandbox Code Playgroud)
我依靠 Django 的查找功能Item根据某些ShelfPosition字段过滤对象:
Item.objects.filter(position__shelf_code="BF4")
在使用get_or_createor时,有什么方法可以实现类似的查找功能,例如上面描述的update_or_create?
item, created = Item.objects.get_or_create(
position__shelf_code="BF6",
position__row=88,
position__column=1,
defaults={……}
)
Run Code Online (Sandbox Code Playgroud)
我发现它比以下内容更简洁,即使与此示例无关:
item, created = Item.objects.get_or_create(
position = Position.objects.get_or_create(
shelf_code="BF6",
row=88,
column=1
),
defaults={……}
)
Run Code Online (Sandbox Code Playgroud) 在我的 Django 2.2 应用程序中,我有一个如下所示的模型:
class Folder(models.Model):
name = models.CharField(max_length=100)
parent_folder = models.ForeignKey("Folder", null=True, on_delete=models.SET_NULL)
Run Code Online (Sandbox Code Playgroud)
基本上,一个文件夹可以有一个父文件夹,该文件夹的父文件夹本身也可以有一个父文件夹,依此类推。
所以我想做的是,根据文件夹的name属性对文件夹进行排序,并从顶级文件夹(因此,没有 的文件夹parent_folder)开始,然后在文件夹层次结构中降序排列。
为了说明这一点,给出表中的以下对象:
+----+---------+---------------+
| id | name | parent_folder |
+----+---------+---------------+
| 1 | fuel | <None> |
| 2 | blabla | 1 |
| 3 | volcano | 2 |
| 4 | awful | 2 |
| 5 | apple | 1 |
| 6 | amazing | <None> |
| 7 | wow | 6 | …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个在编译时执行字符串哈希的类,如果给出了文字字符串,或者运行时(基于本文).我不是像作者那样使用FNV-1a而是使用xxHash(64bits),因为编译时计算我正在使用这段代码.
这是我的实现:
class StringHash {
public:
class ConstCharWrapper {
public:
inline ConstCharWrapper( const char *Str ) : Internal(Str) {}
const char *Internal;
};
template <size_t N>
__forceinline StringHash( const char (&Str)[N] ) :
m_Hash( std::integral_constant<uint64_t, xxh64::hash(Str, N-1)>::value )
{
}
inline StringHash( ConstCharWrapper Str ) :
m_Hash( xxHash_64::Calc((const uint8_t*)Str.Internal, strlen(Str.Internal)) )
{
}
inline StringHash( const char *Str, size_t Length ) :
m_Hash( xxHash_64::Calc((const uint8_t*)Str, Length) )
{
}
__forceinline operator uint64_t() const { …Run Code Online (Sandbox Code Playgroud) 我目前正在对软件进行逆向工程,该软件可以为给定的数据缓冲区计算2字节宽的校验和。该代码来自16位DLL(NE格式),并使用Borland C ++进行了编译。我怀疑校验和为CRC-16,且多边形为0x8408,但是我没有机会计算相同的CRC,所以我想知道实现是否为“ CRC16标准”。
这是程序集的实现:
crc_cal proc far
var_4= word ptr -4
arg_0= word ptr 6
arg_2= dword ptr 8
mov ax, seg dseg37
inc bp
push bp
mov bp, sp
push ds
mov ds, ax
sub sp, 2
push si
push di
xor cx, cx
mov dx, 0FFFFh
mov [bp+var_4], 8408h
loc_42646:
les bx, [bp+arg_2]
add bx, cx
mov al, es:[bx]
xor al, dl
mov dl, al
inc cx
xor di, di
jmp short loc_42672
loc_42657:
mov si, dx …Run Code Online (Sandbox Code Playgroud)