Wrong FieldRowPanel - wagtail

Is my FieldRowPanel wrong?
padding = FieldRowPanel([
FieldPanel(blocks.ChoiceBlock(
required=False, choices=PADDING_CHOICES, label="Отступ сверху")),
FieldPanel(blocks.ChoiceBlock(
required=False, choices=PADDING_CHOICES, label="Отступ снизу")),
], heading="Отступы")
I am trying to do it in this place:
class ContentBlockThemed(blocks.StructBlock):
theme = blocks.ChoiceBlock(
required=True, choices=BLOCK_THEMES_CHOICES, label="Тема")
padding = FieldRowPanel([
FieldPanel(blocks.ChoiceBlock(
required=False, choices=PADDING_CHOICES, label="Отступ сверху")),
FieldPanel(blocks.ChoiceBlock(
required=False, choices=PADDING_CHOICES, label="Отступ снизу")),
], heading="Отступы")
visible = blocks.BooleanBlock(required=False, label="Видимый", default=True, help_text="По умолчанию - True")
block_content = ContentBlock()
class Meta:
icon = 'user'
form_classname = 'content-block struct-block pages-content-block'
template = 'wtblocks/content_block.html'
Nothing is displayed in the admin, although there is no error

This is not valid code.
blocks.ChoiceBlock is only valid inside a StreamField definition
FieldRowPanel and FieldPanel are not valid inside a StreamField definition, only inside the 'panels' definition of a model
FieldPanel should be passed a field name as a string, not a block object
To control the layout of the edit form for a StructBlock (i.e. the equivalent of how you'd use FieldRowPanel in a panel definition), you need to define that layout as an HTML template and set that template as form_template in the block's Meta section.

Related

How to set code view as deafult instead of tree in jsoneditor

https://github.com/mariohmol/ang-jsoneditor.
I have used the above library for jsoneditor.Here I have tree view as default one.But I want code view .How to set it and where do I change it.I am finidng html elements in inspect but in code i am not finding them.here tree view appears when page loads
const container = document.getElementById("id_editor");
var options = {
modes: ['code', 'tree'],
mode: "code",
};
const editor = new JSONEditor(container, options);
Note the mode key (NOT MODES) in the options dictionary.

Cannot make a model #property def-as-field work with wagtail 2.0

I'm using Wagtail 2.0 with a custom Block that has the following code:
class LinkButtonBlock(blocks.StructBlock):
label = blocks.CharBlock()
URL = blocks.CharBlock()
styling = blocks.ChoiceBlock(
choices=[
('btn-primary', 'Primary button'),
('btn-secondary', 'Secondary button'),
('btn-success', 'Success button'),
('btn-info', 'Info button'),
('btn-warning', 'Warning button'),
('btn-error', 'Error button'),
],
default='btn-info',
)
outline = blocks.BooleanBlock(
default=False
)
#property
def css(self):
btn_class = self.styling
if self.outline is True:
btn_class = btn_class.replace('btn-', 'btn-outline-')
return btn_class
class Meta:
icon = 'link'
template = 'testapp/blocks/link_button_block.html'
If I then try to access this css "property" in my template, nothing seems to happen. Putting a print(self) as first line inside the css def also shows nothing on the console suggesting the function never even gets called.
Using the following template:
{% load wagtailcore_tags %}
<a class="btn {{ block.value.css }}" href="{{ block.value.URL }}">{{ block.value.label }}</a>
Simply yields:
<a class="btn " href="actual.url.from.instance">actual.label.from.instance</a>
Also, block.value.styling and block.value.outline on their own work just fine, so... what am I doing wrong here?
The thing that's tripping you up is that the value objects you get when iterating over a StreamField are not instances of StructBlock. Block objects such as StructBlock and CharBlock act as converters between different data representations; they don't hold on to the data themselves. In this respect, they work a lot like Django's form field objects; for example, Django's forms.CharField and Wagtail's CharBlock both define how to render a string as a form field, and how to retrieve a string from a form submission.
Note that CharBlock works with string objects - not instances of CharBlock. Likewise, the values returned from StructBlock are not instances of StructBlock - they are a dict-like object of type StructValue, and this is what you need to subclass to implement your css property. There's an example of doing this in the docs: http://docs.wagtail.io/en/v2.0/topics/streamfield.html#custom-value-class-for-structblock. Applied to your code, this would become:
class LinkButtonValue(blocks.StructValue):
#property
def css(self):
# Note that StructValue is a dict-like object, so `styling` and `outline`
# need to be accessed as dictionary keys
btn_class = self['styling']
if self['outline'] is True:
btn_class = btn_class.replace('btn-', 'btn-outline-')
return btn_class
class LinkButtonBlock(blocks.StructBlock):
label = blocks.CharBlock()
URL = blocks.CharBlock()
styling = blocks.ChoiceBlock(choices=[...])
outline = blocks.BooleanBlock(default=False)
class Meta:
icon = 'link'
template = 'testapp/blocks/link_button_block.html'
value_class = LinkButtonValue

Get complerte related model details with model id: Django REST

I have two models User and Item.
The model Item is
class Item
owner = models.ForeignKey(User, related_name='items',null=True)
A user will have more than one item. The user serializer is
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id','username','items')
So that i can access the list of all the items of a user. But here the field items returns only the 'item id', I need the whole item model related to a user. How can i get it?
For customization list of related objects (items) you can use SerializerMethodField. I prefer write another serializer for Item objects and use it for serializing in this method.
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
class UserSerializer(serializers.ModelSerializer):
items = serializers.SerializerMethodField()
def get_items(self, obj):
return ItemSerializer(obj.items, many=True).data
class Meta:
model = User
fields = ('id','username','items')

how to set a selected text with a inline <span> class?

I would like to wrap a text with .
I've attempted to do it via extending the Inline class.
i.e.
class MBlot extends Inline { }
MBlot.blotName = 'ql-important';
MBlot.tagName = 'span';
MBlot.className = 'important'
Quill.register(MBlot)
Quill.formatText(0, 5, 'ql-important', true);
However, this has no effect.
BTW: If i change the tagName to "someOtherTag", it seems to work
How should i go about it?

How can I pass a parameter to an ng-click function?

I have a function in my controller that looks like the following:
AngularJS:
$scope.toggleClass = function(class){
$scope.class = !$scope.class;
}
I want to keep it general by passing the name of the class that I want to toggle:
<div class="myClass">stuff</div>
<div ng-click="toggleClass(myClass)"></div>
But myClass is not being passed to the angular function. How can I get this to work? The above code works if I write it like this:
$scope.toggleClass = function(){
$scope.myClass = !$scope.myClass;
}
But, this is obviously not general. I don't want to hard-code in the class named myClass.
In the function
$scope.toggleClass = function(class){
$scope.class = !$scope.class;
}
$scope.class doesn't have anything to do with the paramter class. It's literally a property on $scope called class. If you want to access the property on $scope that is identified by the variable class, you'll need to use the array-style accessor:
$scope.toggleClass = function(class){
$scope[class] = !$scope[class];
}
Note that this is not Angular specific; this is just how JavaScript works. Take the following example:
> var obj = { a: 1, b: 2 }
> var a = 'b'
> obj.a
1
> obj[a] // the same as saying: obj['b']
2
Also, the code
<div ng-click="toggleClass(myClass)"></div>
makes the assumption that there is a variable on your scope, e.g. $scope.myClass that evaluates to a string that has the name of the property you want to access. If you literally want to pass in the string myClass, you'd need
<div ng-click="toggleClass('myClass')"></div>
The example doesn't make it super clear which you're looking for (since there is a class named myClass on the top div).

Resources