Log in

Topic Custom field

  • 7 Mar '16

Hi,
First, thank you for this app!

I would like to know which is the preferred way to add a custom field to a topic.
I would like to add a geo location to a topic.
Is the recommended way to directly modify the model, the form and the template?

I would like to minimize efforts in future Spirit updates. So, maybe an alternative solution could be intercepting a Topic post_save signal, and accessing the request object for extra information (set by some JavaScript before submitting the Topic form), but I do not know if this is right/possible.

Sorry if this is just a django related question.

nitelyEsteban Castro Borsani
  • 2
  • 8 Mar '16

@andreav said:
I would like to know which is the preferred way to add a custom field to a topic.
I would like to add a geo location to a topic.
Is the recommended way to directly modify the model, the form and the template?

At the moment I'd say yes, that's the current way to go.

I would like to minimize efforts in future Spirit updates. So, maybe an alternative solution could be intercepting a Topic post_save signal, and accessing the request object for extra information (set by some JavaScript before submitting the Topic form), but I do not know if this is right/possible.

Well it depends. If the field can be modified by a user then no, coz you won't have a way to tell the user the field has a error. Forking the repo is the only way.

If you are implementing a pure JS solution that does not requires user interaction, then yes, it would be possible but not with a post_save signal since it does not have a request param

So you would have to fork the repo, add the signals (for topic.views.publish) and create a pull request.

You would also have to create a model that extends the topic-model (OneToOneField), a form to validate the data (altough if not valid then the default field value would be saved, since there is no way to tell the user the field has an error) and override the template to add the form (a templatetag would work).

FWIW there have been some efforts to improve the situation, but it's not there yet (ie: no way to extend models).

  • 8 Mar '16

First, thank you for your response.

About accessing the request param from post_save signal, I found this middleware could solve the problem. ( ref - stack overflow )
However I'm not able to solve my problem following that way.
Above all, you are right about the validation problem (i.e. telling the user the field has a error)

So I choose this way:

  • modify spirit.topic.views.publish method for accepting some new optional parameters: FormClass, template, extra_context and extra_init
  • define a model (say "TopicExt") for storing this extra data (i.e. a PointField) and a OneToOneField poiting to Topic
  • define a custom form inheriting from TopicForm with my extra field(s), say it "TopicAndPointForm"
  • invoke spirit.topic.views.publish with FormClass=TopicAndPointForm
  • override TopicAndPointForm::save method for creating a TopicExt object after saving Topic object
  • modifying the template spirit/topic/publish.html in order to add {{ form.media }} (my point widget needed some js)

At the moment I can successfully:

  • GET the publish page with my extra map
  • POST the topic with my extra location
  • Successfully create a topic and a TopicExt object

Do you agree? Do you see any drawbacks?

Thank you!

nitelyEsteban Castro Borsani
  • 8 Mar '16

If you are modifying the view that way, I would just add the extra form without mixing it with the topicform, but that is just me, I see no drawbacks in your approach that I can think of.

  • 8 Mar '16

The only advantage I see is I can now create many forms (say TopicAndPointForm, TopicAndTripForm ... ) and reuse the same view.
And I have not to modify the template (except for the {{ form.media }} stuff).
Thank you!
This is a great app, feature full and I set it up in a few minutes (other forums are so hard to setup!)

nitelyEsteban Castro Borsani
  • 1
  • 9 Mar '16

The only advantage I see is I can now create many forms (say TopicAndPointForm, TopicAndTripForm ... ) and reuse the same view.
And I have not to modify the template (except for the {{ form.media }} stuff).

That is true, you would have to touch the view and template every time you add a new form. On the other hand you avoid the (hard to test) deep inheritance hierarchy. Either way the fact that Spirit is hard to extend remains the same.

Thank you!
This is a great app, feature full and I set it up in a few minutes (other forums are so hard to setup!)

, some people would say the opposite haha

Reply