如何在QoR模型中只读取字段?

时间:2017-04-24 21:11:48

标签: go qor

我有一个数据模型:

void *connectionThread(void *socket_desc){

    char buffer[1024];
    int newsockfd = *(int*)socket_desc;
    int n;
    magic_t myt = magic_open(MAGIC_ERROR|MAGIC_MIME_TYPE);
    magic_load(myt,NULL);
    bzero(buffer,256);
    FILE * picture;
    int size;
    char *str_size = malloc(100);
    struct stat sb;
    int fd = open("TulsaQueen.mp3", O_RDONLY );
    fstat( fd, &sb );

 while (1)
     {memset(buffer, 0, 1024);n = read(newsockfd,buffer,1024);
     if (n < 0){ printf("ERROR reading from socket"); close(newsockfd);pthread_exit(NULL);}
     printf("%s",buffer);


    char *token = strtok(buffer," ");
    if(token != NULL)
    token = strtok(NULL, " ");

    printf("%s\n",token);

    if(strcmp(magic_file(myt,token),"audio/mpeg") == 0){

        write(newsockfd,"HTTP/1.1 200 OK\r\n",strlen("HTTP/1.1 200 OK\r\n"));
        write(newsockfd,"Content-Length: ",strlen("Content-Length: ")); 
        snprintf(str_size,100,"%li",sb.st_size);
        write(newsockfd,str_size,strlen(str_size));
        write(newsockfd,"\r\n",strlen("\r\n"));
        write(newsockfd,"Content-Type: ",strlen("Content-type: "));
        printf("magic output: '%s'\n",magic_file(myt,token));
        write(newsockfd,magic_file(myt,token),strlen(magic_file(myt,token))); 
        write(newsockfd,"\r\nConnection: keep-alive",strlen("\r\nConnection: keep-alive"));
        write(newsockfd,"\r\n\r\n",strlen("\r\n\r\n"));

        write( newsockfd, &sb.st_size, sizeof( sb.st_size ) );
        sendfile( newsockfd, fd, 0, sb.st_size );
    }
    if(strcmp(magic_file(myt,token),"image/jpeg") == 0){
    picture = fopen (token, "r");
    fseek(picture,0,SEEK_END);
    size = ftell(picture);
    fseek(picture,0,SEEK_SET);
    char file_buf[size];
    snprintf(str_size,16,"%d",size);

    //Header for HTTP standards
    write(newsockfd,"HTTP/1.1 200 OK\r\n",strlen("HTTP/1.1 200 OK\r\n"));
    write(newsockfd,"Content-Length: ",strlen("Content-Length: "));
    write(newsockfd,str_size,strlen(str_size));
    write(newsockfd,"\r\n",strlen("\r\n"));
    write(newsockfd,"Content-Type: ",strlen("Content-type: "));
    write(newsockfd,magic_file(myt,token),strlen(magic_file(myt,token))); //get Content-type
    write(newsockfd,"\r\nConnection: keep-alive",strlen("\r\nConnection: keep-alive"));
    write(newsockfd,"\r\n\r\n",strlen("\r\n\r\n"));

    rewind(picture);
    while(!feof(picture)){
    fread(file_buf, sizeof(char), sizeof(file_buf), picture);
        write(newsockfd, file_buf, sizeof(file_buf));
    bzero(file_buf, sizeof(file_buf));
    }
    }

     }  
    close(fd);
    free(buffer);
    fclose(picture);
    close(newsockfd);
    magic_close(myt);
    pthread_exit(NULL);
    return 0;
}

我在菜单中发布了这个数据模型:

var data = [{"id":"10","data":"12345"},{"id":"11","data":"6666"}];

for(var row in data)
{
    var item = data[row]; //This is the object {id:..., data:...}

    if(item.id == ...) //Use if statement to search for data
    {
       //do something here when data is found
    }
}

现在,我可以通过QoR管理面板查看和编辑数据。但我希望将值type Epg struct { gorm.Model Uri string `gorm:";not null;unique"` Prefix string `gorm:"size:64;not null;default:''"` Etag string Updated time.Time Status bool `gorm:"default:true"` } EpgResource := Admin.AddResource(&models.Epg{}, &admin.Config{Menu: []string{"Content"}}) Etag设为只读,因为它们会被系统更新。

如果我尝试根据documentation只读取此字段:

Updated

我收到了一个错误。是否可以使数据模型中的某些字段可见但只读?怎么做?

错误日志跟踪。

  

2017/04/25 01:16:04完成[GET] / admin / epgs达到19.59ms   /usr/local/go/src/text/template/exec.go:433   /usr/local/go/src/text/template/exec.go:536   /usr/local/go/src/text/template/exec.go:668   /usr/local/go/src/reflect/value.go:302   /usr/local/go/src/reflect/value.go:434   /usr/local/go/src/runtime/asm_amd64.s:515   /home/rns/golang/src/github.com/qor/admin/func_map.go:1051   /home/rns/golang/src/github.com/qor/admin/func_map.go:220   /home/rns/golang/src/github.com/qor/admin/func_map.go:236   /home/rns/golang/src/github.com/qor/admin/func_map.go:393   Etag(Readonly)的渲染表单模板时出错:找不到表单   meta Etag的模板

2 个答案:

答案 0 :(得分:1)

qor未在.../metas/form/Etag.tmpl找到模板文件导致该特定错误,这可能是实际将Etag呈现给表单所需的。 (您可以使模板呈现只读/静态元素而不是输入)

答案 1 :(得分:0)

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "Readonly"})仅可在darwin(Mac)计算机上使用。但是,不是在Linux机器上。

执行以下操作:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "readonly"})

我希望这会有所帮助。

参考:https://doc.getqor.com/admin/metas/hidden-readonly.html#readonly