Django Concat columns with integers and strings












0















I have run into an issue using attempting to add values from two different columns together in a query, namely that some of them contain numbers. This means that the built in Concat does not work as it requires strings or chars.



Considering how one can cast variables as other datatypes in SQL I don't see why I wouldn't be able to do that in Django.



cast(name as varchar(100))


I would assume that one would do it as follows in Django using the Concat function in combination with Cast.



queryset.annotate(new_col=Concat('existing_text_col', Cast('existing_integer_col', TextField())).get())


The above obviously does not work, so does anyone know how to actually do this?



The use case if anyone wonders are sending jenkins urls saved as fragments as a whole. So one url would be:



base_url: www.something.com/
url_fragment: name/
url_number: 123456









share|improve this question























  • Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

    – Daniel Roseman
    Nov 13 '18 at 16:14











  • No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

    – Catfish
    Nov 14 '18 at 7:14


















0















I have run into an issue using attempting to add values from two different columns together in a query, namely that some of them contain numbers. This means that the built in Concat does not work as it requires strings or chars.



Considering how one can cast variables as other datatypes in SQL I don't see why I wouldn't be able to do that in Django.



cast(name as varchar(100))


I would assume that one would do it as follows in Django using the Concat function in combination with Cast.



queryset.annotate(new_col=Concat('existing_text_col', Cast('existing_integer_col', TextField())).get())


The above obviously does not work, so does anyone know how to actually do this?



The use case if anyone wonders are sending jenkins urls saved as fragments as a whole. So one url would be:



base_url: www.something.com/
url_fragment: name/
url_number: 123456









share|improve this question























  • Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

    – Daniel Roseman
    Nov 13 '18 at 16:14











  • No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

    – Catfish
    Nov 14 '18 at 7:14
















0












0








0








I have run into an issue using attempting to add values from two different columns together in a query, namely that some of them contain numbers. This means that the built in Concat does not work as it requires strings or chars.



Considering how one can cast variables as other datatypes in SQL I don't see why I wouldn't be able to do that in Django.



cast(name as varchar(100))


I would assume that one would do it as follows in Django using the Concat function in combination with Cast.



queryset.annotate(new_col=Concat('existing_text_col', Cast('existing_integer_col', TextField())).get())


The above obviously does not work, so does anyone know how to actually do this?



The use case if anyone wonders are sending jenkins urls saved as fragments as a whole. So one url would be:



base_url: www.something.com/
url_fragment: name/
url_number: 123456









share|improve this question














I have run into an issue using attempting to add values from two different columns together in a query, namely that some of them contain numbers. This means that the built in Concat does not work as it requires strings or chars.



Considering how one can cast variables as other datatypes in SQL I don't see why I wouldn't be able to do that in Django.



cast(name as varchar(100))


I would assume that one would do it as follows in Django using the Concat function in combination with Cast.



queryset.annotate(new_col=Concat('existing_text_col', Cast('existing_integer_col', TextField())).get())


The above obviously does not work, so does anyone know how to actually do this?



The use case if anyone wonders are sending jenkins urls saved as fragments as a whole. So one url would be:



base_url: www.something.com/
url_fragment: name/
url_number: 123456






sql django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 12:53









CatfishCatfish

257




257













  • Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

    – Daniel Roseman
    Nov 13 '18 at 16:14











  • No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

    – Catfish
    Nov 14 '18 at 7:14





















  • Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

    – Daniel Roseman
    Nov 13 '18 at 16:14











  • No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

    – Catfish
    Nov 14 '18 at 7:14



















Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

– Daniel Roseman
Nov 13 '18 at 16:14





Is there any reason to do this in SQL rather than directly in Django? ie os.path.join(item.base_url, item.url_fragment, str(item.url_number))?

– Daniel Roseman
Nov 13 '18 at 16:14













No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

– Catfish
Nov 14 '18 at 7:14







No reason to do it in SQL when having installed a framework. Sorry if I wrote my question oddly. I just gave an example in SQL since I am more comfortable with it than Django. I don't recognize os.path so I will have to look into that, but perhaps it's just what I need @DanielRoseman

– Catfish
Nov 14 '18 at 7:14














1 Answer
1






active

oldest

votes


















0














I ended up writing a serializer that inherits from the base serializer that contains the urls fragments and a lot of other things. In it I made a MethodField for my complete url and defined a getter function that loaded in the different fragments and added them together. I also redeclared the fragmented fields to None.



The code inside the new serializer is:



complete = serpy.MethodField("get_copmlete")

serverUrl = serpy.Field(attr=None, call=False, required=False)
jobName = serpy.Field(attr=None, call=False, required=False)
buildNumber = serpy.Field(attr=None, call=False, required=False)

def get_complete(self, obj):
return obj.server_url + obj.job_name + '/' + str(obj.build_number)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281464%2fdjango-concat-columns-with-integers-and-strings%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I ended up writing a serializer that inherits from the base serializer that contains the urls fragments and a lot of other things. In it I made a MethodField for my complete url and defined a getter function that loaded in the different fragments and added them together. I also redeclared the fragmented fields to None.



    The code inside the new serializer is:



    complete = serpy.MethodField("get_copmlete")

    serverUrl = serpy.Field(attr=None, call=False, required=False)
    jobName = serpy.Field(attr=None, call=False, required=False)
    buildNumber = serpy.Field(attr=None, call=False, required=False)

    def get_complete(self, obj):
    return obj.server_url + obj.job_name + '/' + str(obj.build_number)





    share|improve this answer




























      0














      I ended up writing a serializer that inherits from the base serializer that contains the urls fragments and a lot of other things. In it I made a MethodField for my complete url and defined a getter function that loaded in the different fragments and added them together. I also redeclared the fragmented fields to None.



      The code inside the new serializer is:



      complete = serpy.MethodField("get_copmlete")

      serverUrl = serpy.Field(attr=None, call=False, required=False)
      jobName = serpy.Field(attr=None, call=False, required=False)
      buildNumber = serpy.Field(attr=None, call=False, required=False)

      def get_complete(self, obj):
      return obj.server_url + obj.job_name + '/' + str(obj.build_number)





      share|improve this answer


























        0












        0








        0







        I ended up writing a serializer that inherits from the base serializer that contains the urls fragments and a lot of other things. In it I made a MethodField for my complete url and defined a getter function that loaded in the different fragments and added them together. I also redeclared the fragmented fields to None.



        The code inside the new serializer is:



        complete = serpy.MethodField("get_copmlete")

        serverUrl = serpy.Field(attr=None, call=False, required=False)
        jobName = serpy.Field(attr=None, call=False, required=False)
        buildNumber = serpy.Field(attr=None, call=False, required=False)

        def get_complete(self, obj):
        return obj.server_url + obj.job_name + '/' + str(obj.build_number)





        share|improve this answer













        I ended up writing a serializer that inherits from the base serializer that contains the urls fragments and a lot of other things. In it I made a MethodField for my complete url and defined a getter function that loaded in the different fragments and added them together. I also redeclared the fragmented fields to None.



        The code inside the new serializer is:



        complete = serpy.MethodField("get_copmlete")

        serverUrl = serpy.Field(attr=None, call=False, required=False)
        jobName = serpy.Field(attr=None, call=False, required=False)
        buildNumber = serpy.Field(attr=None, call=False, required=False)

        def get_complete(self, obj):
        return obj.server_url + obj.job_name + '/' + str(obj.build_number)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 8:06









        CatfishCatfish

        257




        257






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281464%2fdjango-concat-columns-with-integers-and-strings%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python