小编fh_*_*ash的帖子

如何在django中设置和获取会话变量?

当用户登录时,我需要在会话上设置变量.我怎样才能做到这一点?

if request.user.is_authenticated():
profile = request.user.get_profile()
request.session['idempresa'] = profile.idempresa
Run Code Online (Sandbox Code Playgroud)

我的另一个问题是:

class PedidoItensForm(ModelForm):
class Meta:
    model = ItensPedido

def __init__(self, *args, **kwargs):
    profile = kwargs.pop('vUserProfile', None)
    super(PedidoItensForm, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

如何在查询集中使用session的"idempresa"值?

django session

45
推荐指数
2
解决办法
5万
查看次数

Django表单__init __()获得了关键字参数的多个值

您好,我正在尝试使用修改后的__init__表单方法,但我遇到以下错误:

TypeError
__init__() got multiple values for keyword argument 'vUserProfile'
Run Code Online (Sandbox Code Playgroud)

我需要传递UserProfile给我的表单,才能进入dbname现场,我认为这是一个解决方案(我的表单代码):

class ClienteForm(ModelForm):
class Meta:
    model = Cliente

def __init__(self, vUserProfile, *args, **kwargs):
    super(ClienteForm, self).__init__(*args, **kwargs)
    self.fields["idcidade"].queryset = Cidade.objects.using(vUserProfile.dbname).all()
Run Code Online (Sandbox Code Playgroud)

ClienteForm()没有POST的情况下调用构造函数是成功的,并向我显示正确的表单.但是当提交表单并使用POST调用构造函数时,我得到之前描述的错误.

django-forms

25
推荐指数
2
解决办法
3万
查看次数

如何在postgresql中将json对象作为列?

我在mu PostgreSQL 9.05上有这些表:

表:core 字段:name,description,data

data field是一个json字段,有(例如): {"id": "100", "tax": "4,5"}

one每个数据始终是json.

我的问题是:我可以将所有JSON字段作为查询字段吗?像这样返回:name, description, id, tax....

问题是:我的JSON确实有各种字段,可以是Id,税或其他.

postgresql json

11
推荐指数
2
解决办法
1万
查看次数

如何在模型中直接使用大写的所有CharField?

在我的所有Django模型中,我尝试在所有CharField中使用UpperCase.

今天我的save方法中有一些代码:

def save(self, *args, **kwargs):
        for field_name in ['razao_social', 'nome_fantasia', 'cidade', 'endereco','bairro', 'uf', 'cli_parc_nomeparc', 'cli_repr_nomerepr']:
            val = getattr(self, field_name, False)
            if val:
                setattr(self, field_name, val.upper())
        super(Pessoa, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

但它需要一些时间.有什么方法可以在我的模型中放置一些大写= True?

谢谢.

django models

4
推荐指数
3
解决办法
8554
查看次数

如何在运行时更改 django 中的数据库

我的用户配置文件中有一个字段来定义数据库名称,该用户可以连接。

像这样:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    dbname = models.CharField(u'Database name', max_length=100, null=True, blank=True)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)
Run Code Online (Sandbox Code Playgroud)

如何设置 - 在运行时 - django 使用在我的 UserProfile 中预先配置的数据库名称?

谢谢

python database django login

1
推荐指数
1
解决办法
5795
查看次数

标签 统计

django ×3

database ×1

django-forms ×1

json ×1

login ×1

models ×1

postgresql ×1

python ×1

session ×1