首页 > 解决方案 > django模型字段获取id字段的默认值

问题描述

大家好,我想在地理服务器的地图中显示图层的标签,但地理服务器不允许我将 id 字段显示为标签。我创建了一个新值 no_man 并希望该字段自动获取 id 的值。如果有办法显示 id 标签,我会删除 no_man,否则我想用 id 的值填充这个字段。

模型.py

class ww_manholes(models.Model):
    id = models.BigIntegerField(primary_key=True)
    no_man = models.BigIntegerField()
    l_ass_obj = models.BigIntegerField()
    l_field_re = models.BigIntegerField()
    street = models.BigIntegerField()
    zip_code = models.BigIntegerField()
    regio_code = models.BigIntegerField()
    owner = models.BigIntegerField()
    ww_type = models.BigIntegerField()
    accuracy = models.BigIntegerField()
    x = models.FloatField()
    y = models.FloatField()
    x_ge = models.FloatField()
    y_ge = models.FloatField()
    z = models.FloatField()
    invert_el = models.FloatField()
    inv_start = models.FloatField()
    inv_end = models.FloatField()
    depth = models.FloatField()
    depth_star = models.FloatField()
    depth_end = models.FloatField()
    material = models.BigIntegerField()
    lining = models.BigIntegerField()
    coating = models.BigIntegerField()
    contractor = models.BigIntegerField()
    const_year = models.BigIntegerField()
    diam_nom = models.BigIntegerField()
    diam_inner = models.BigIntegerField()
    joint_type = models.BigIntegerField()
    cover_dim = models.BigIntegerField()
    cover_mat = models.BigIntegerField()
    access = models.BigIntegerField()
    rough_coef = models.FloatField()
    slope = models.FloatField()
    type = models.BigIntegerField()
    sc_condit = models.BigIntegerField()
    sc_perform = models.BigIntegerField()
    sc_fail_pr = models.BigIntegerField()
    sc_fin_imp = models.BigIntegerField()
    sc_soc_imp = models.BigIntegerField()
    sc_leg_imp = models.BigIntegerField()
    sc_env_imp = models.BigIntegerField()
    sc_red_sl = models.BigIntegerField()
    sc_imp_ph = models.BigIntegerField()
    sc_imp_rep = models.BigIntegerField()
    sc_rep_cos = models.BigIntegerField()
    sc_co_fail = models.BigIntegerField()
    l_picture = models.CharField(max_length=254)
    l_drawing = models.CharField(max_length=254)
    key_om = models.FloatField()
    comments = models.CharField(max_length=254)
    geom = models.MultiPointField(srid=7392)

    def __unicode__(self):
        return self.id

标签: djangoleafletgeoserver

解决方案


将此添加到您的模型将主要解决您的问题。

def __str__(self):
    return self.id

更新:按要求

不推荐,但这是您可以使用Django 信号复制字段值的方式:

@receiver(post_save, sender= ww_manholes)
def copy_id(sender, **kwargs):
     data = ww_manholes.objects.get(id=kwargs.get('instance').id)
     data.no_man = data.id
     data.save()

推荐阅读