小编Vin*_*uki的帖子

如何使用元类拦截类创建和添加属性?

在下面的代码,我想元类NameMeta以属性添加genderMyName类的情况下,此类不声明属性.

class NameMeta(type):
    def __new__(cls, name, bases, dic):
        if 'gender' not in dic:
            setattr(name, 'gender', 'Male')
        return super().__new__(cls, name, bases, dic)

class MyName(metaclass=NameMeta):
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname       
    def fullname(self):
        self.full_name = self.fname + self.lname
        return self.full_name 
inst = MyName('Joseph ', 'Vincent')
print(MyName.gender)
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

<ipython-input-111-550ff3cfae41> in __new__(cls, name, bases, dic)
      2     def __new__(cls, name, bases, dic):
      3         if 'gender' not in dic:
----> 4             setattr(name, 'gender', 'Male')
      5         return …
Run Code Online (Sandbox Code Playgroud)

python

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

有没有办法让石墨烯与 django GenericRelation 字段一起使用?

我有一些 django 模型通用关系字段,我希望它们出现在 graphql 查询中。石墨烯支持通用类型吗?

class Attachment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    file = models.FileField(upload_to=user_directory_path)
Run Code Online (Sandbox Code Playgroud)
class Aparto(models.Model):
    agency = models.CharField(max_length=100, default='Default')
    features = models.TextField()
    attachments = GenericRelation(Attachment)
Run Code Online (Sandbox Code Playgroud)

石墨烯类:

class ApartoType(DjangoObjectType):
    class Meta:
        model = Aparto
Run Code Online (Sandbox Code Playgroud)
class Query(graphene.ObjectType):
    all  = graphene.List(ApartoType)
    def resolve_all(self, info, **kwargs):
        return Aparto.objects.all()

schema = graphene.Schema(query=Query)
Run Code Online (Sandbox Code Playgroud)

我希望附件字段出现在 graphql 查询结果中。仅显示代理和功能。

python django graphql graphene-python

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

在 Angular &gt;=6 模板中扩展元素的属性

我的代码中有这个...

@Component({
  selector: 'generic-input',
  template: `<div><input [formControl]="control"/></div>`,
})
export class GenericInputComponent implements OnInit {

  @Input('config') config = {placeholder: 'Testability', disabled: true, type: 'text'};

  control;

  constructor() { }

  ngOnInit() {
    this.control = new FormControl();
  }

}
Run Code Online (Sandbox Code Playgroud)

我想通过使用某种循环或其他方式来传播配置对象中的属性,以便呈现的 html 如下所示:

<div><input placeholder='Testability', disabled=true type='text' [formControl]="control"/></div>
Run Code Online (Sandbox Code Playgroud)

注意: 以下不是一个选项:

   <div><input [placeholder]='config.placeholder', [disabled]='config.disabled' [formControl]="control"/></div>
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助和想法。谢谢。

javascript angular

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

标签 统计

python ×2

angular ×1

django ×1

graphene-python ×1

graphql ×1

javascript ×1