Trying to create a CBV that allows user to copy a record

30 views
Skip to first unread message

Alexander Joseph

unread,
Feb 28, 2018, 12:42:48 PM2/28/18
to Django users
I'd like to create a CBV to allow users to copy a record, except for item specific information. - I'm making a fixed assets app and if there are 2 of the same laptop I'd like the user to be able to make one record in the database, then copy it minus the serial number and asset tag number which would be the only info the user would have to fill in to copy that laptop.

Is there a way to do this by modifying the CreateView somehow? Thanks

Dylan Reinhold

unread,
Feb 28, 2018, 1:53:01 PM2/28/18
to django...@googlegroups.com
Use the get_inital method.

    def get_initial(self):
        initial = super(MyCreateView, self).get_initial()
        initial['name'] = 'Testing'
        return initial

Dylan


On Wed, Feb 28, 2018 at 9:42 AM, Alexander Joseph <alexander...@gmail.com> wrote:
I'd like to create a CBV to allow users to copy a record, except for item specific information. - I'm making a fixed assets app and if there are 2 of the same laptop I'd like the user to be able to make one record in the database, then copy it minus the serial number and asset tag number which would be the only info the user would have to fill in to copy that laptop.

Is there a way to do this by modifying the CreateView somehow? Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e4c25ff6-9e6e-4e5d-898a-f6d977f9badc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Joseph

unread,
Mar 1, 2018, 9:57:13 AM3/1/18
to Django users
Thanks!


On Wednesday, February 28, 2018 at 11:53:01 AM UTC-7, Dylan Reinhold wrote:
Use the get_inital method.

    def get_initial(self):
        initial = super(MyCreateView, self).get_initial()
        initial['name'] = 'Testing'
        return initial

Dylan

On Wed, Feb 28, 2018 at 9:42 AM, Alexander Joseph <alexander...@gmail.com> wrote:
I'd like to create a CBV to allow users to copy a record, except for item specific information. - I'm making a fixed assets app and if there are 2 of the same laptop I'd like the user to be able to make one record in the database, then copy it minus the serial number and asset tag number which would be the only info the user would have to fill in to copy that laptop.

Is there a way to do this by modifying the CreateView somehow? Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Alexander Joseph

unread,
Mar 5, 2018, 10:39:34 AM3/5/18
to Django users
I'm trying to figure out how get_initial will work in this instance. I know I will have to pass the asset_tag.pk through the url to the FixedAssetCopyView but not sure what to set for my initial data. Assuming I just want to set the "manufacturer" = to the manufacturer of the fixed_asset.pk that gets passed through the url, what would I put in my view? Also does my url look right? Thanks


This is my View

class FixedAssetCopyView(LoginRequiredMixin, CreateView):
    fields = ("asset_tag=''", "serial_number=''", "manufacturer", "model_number", "description", "purchase_date", "purchase_price", "purchased_from", "purchase_condition", "asset_type", "owner", "location", "active", "inactive_date", "comments")
    model = FixedAsset
    template_name = 'administration/fixed_assets/fixed_asset_form.html'

    def get_initial(self):
        initial = super().get_initial()
        initial["manufacturer"] = fixed_asset.pk.manufacturer
        return initial



And this is my url

url(r'^fixed_assets/copy/(?P<pk>\d+)/$', fixed_assets.FixedAssetCopyView.as_view(), name='fixed_asset_copy'),


Reply all
Reply to author
Forward
0 new messages